Dave's Brain

Browse - programming tips - stl get iterator from insert

Date: 2007dec6
Language: C/C++

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;
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, 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.
Advertisements: