Dave's Brain

Browse - programming tips - stl get iterator from insert

Date: 2007dec6
Language: C++

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

A.  Yes.  Here's how:
	
	#include <map>
	#include <string>

	typedef std::map<std::string, int> MYMAP;
	MYMAP			mymap;

	MYMAP::_Pairib		pair;
	MYMAP::iterator		it;

	// insert() returns a pair
	pair = mymap.insert(MYMAP::value_type("hello", 4));
	it = pair.first;

	// Now you can use "it", an iterator pointing to the just inserted item

Add a comment

Sign in to add a comment
Copyright © 2008, dave - Code on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License.