This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
operator[] and std::map
- From: Gilbert Laycock <gtl1 at mcs dot le dot ac dot uk>
- To: libstdc++ at gcc dot gnu dot org
- Date: Fri, 21 Mar 2003 13:17:16 +0000
- Subject: operator[] and std::map
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 at mcs dot le dot ac dot uk
Maths and Computer Science, http://www.mcs.le.ac.uk/~glaycock
University of Leicester phone: (+44) 116 252 3902
UK GPG: 7D025C00