Dave's Brain

Browse - Programming Tips - Can I get an a iterator that points to the item I just inserted into a map?

Date: 2007dec6
Language: C/C++
Keywords: insert

Q.  Can I get an a iterator that points to the item I just inserted into a map?

A.  Yes.  Here's how:

Microsoft Visual C++:

#include <map> #include <string> typedef std::map<std::string, int> MYMAP; MYMAP mymap; MYMAP::_Pairib result; MYMAP::iterator it; // insert() returns a pair result = mymap.insert(MYMAP::value_type("hello", 4)); it = result.first; // Now you can use "it", an iterator pointing to the just inserted item
Borland C++ 5.02:
std::pair<MYMAP::iterator, bool> result = mymap.insert(MYMAP::value_type("hello", 4)); it = result.first;

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.