ccplusplus.com
Learn C, C++ Concepts
Tuesday, January 3, 2012
stl map example in c++
/*********************************************************************** * File : map.cpp * Author : Saurabh Gupta * Desc : stl map example in c++ * stl map c++ * stl multimap example in c++ * Source : http://www.ccplusplus/2011/10/standard-template-library.html * Created : PM 12:12 03 January 2012 * 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++
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment