ccplusplus.com
Learn C, C++ Concepts
Thursday, December 1, 2011
stl map example
/*********************************************************************** * File : map.cpp * Author : Saurabh Gupta * Desc : map example c++ * stl map c++ * stl multimap example in c++ * Source : http://saurabhgupta0527.blogspot.com/2011/10/standard-template-library.html * Created : AM 12:53 27 November 2011 * Note : ************************************************************************/ #include <iostream> #include <map> #include <string> using namespace std; int main() { //type of the collection typedef map<int, string> IntStringMMap; IntStringMMap coll; //set container for int/string values //insert some elements in arbitrary order //- a value with key 1 gets inserted twice coll.insert(make_pair(5,"tagged")); coll.insert(make_pair(2,"a")); coll.insert(make_pair(1,"this")); coll.insert(make_pair(4,"of")); coll.insert(make_pair(6,"strings")); coll.insert(make_pair(1,"is")); coll.insert(make_pair(3,"map")); /* print all element values *- iterate over all elements *- element member second is the value */ IntStringMMap::iterator pos; for (pos = coll.begin(); pos != coll.end(); ++pos) { cout << pos->second << ' '; } cout << endl; } /* * OUTPUT * [sgupta@rhel6x64 stl]$ c++ map.cpp -o map [sgupta@rhel6x64 stl]$ ./map this is a map of tagged strings [sgupta@rhel6x64 stl]$ */
See Also:
Standard Template Library Concepts and sample codes in C++
1 comment:
Unknown
May 3, 2013 at 12:02 PM
this a map of tagged strings ..
output is not correct :)
Reply
Delete
Replies
Reply
Add comment
Load more...
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
this a map of tagged strings ..
ReplyDeleteoutput is not correct :)