Dave's Brain

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); }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.