Dave's Brain

Browse - programming tips - stl map insert

Date: 2006Jan10
Language: C/C++

Q. What's the best way to insert into an STL map?

A. Assuming these declarations:

	typedef std::map<std::string, int, std::less<std::string> > StringToInt;
	StringToInt	m;

This will usually work:

	m["one"] = 1;

But this is more clear:

	m.insert(StringToInt::value_type("one", 1));

It has no chance of being confused with a fetch from the map.
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: