operator[] and std::map
Gilbert Laycock
gtl1@mcs.le.ac.uk
Fri Mar 21 13:17:00 GMT 2003
Should it be possible to use operator[] from std::map in a const
context?
For example, in class A, the isFive method will not compile (g++
3.2.3) because _m[c] does not preserve const. In contrast std::vector
provides a const version of operator[].
#include <map>
class A {
public:
A () : _m() {}
void set (char c, int v) {
_m[c] = v;
}
bool isFive (char c) const {
return _m[c] == 5;
}
private:
std::map<char, int> _m;
};
--
Gilbert Laycock email: gtl1@mcs.le.ac.uk
Maths and Computer Science, http://www.mcs.le.ac.uk/~glaycock
University of Leicester phone: (+44) 116 252 3902
UK GPG: 7D025C00
More information about the Libstdc++
mailing list