Browse - Programming Tips - How do I convert a std::map iterator to a pointer?

Date: 2015apr20
Language: C/C++

Q.  How do I convert a std::map<> iterator to a pointer?

A.  Use const_cast<MyStruct*>(&it->second) as we show here:

struct MyStruct { // stuff } typedef std::map<std::string, MyStruct> MyMap; MyStruct *getPointer(MyMap map, const char *key) { MyMap::iterator it = map.find(key); if (it == map.end()) return NULL; return const_cast<MyStruct*>(&it->second); }