Maps n Multimaps

Maps n Multimaps - Capacity: empty Test whether map is...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Click to edit Master subtitle style 5/12/09 Map and Multimap (STL)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
5/12/09 Map Maps are a kind of associative containers that stores elements by a key value and a mapped value. A example of a map is a telephone book where the name is the key and the telephone number is the mapped value. The elements in the map are sorted from lower to higher key values.
Background image of page 2
5/12/09 Map examples Example 1: #include <map> map<string, int> grades; grades["Homer"] = 94; grades["Marge"] = 95; grades["Lisa"] = 80; grades["Maggie"] = 84; grades["Bart"] = 30; Example 2: #include <map> map<string, int> grades; grades.insert ( pair<string,int>(“Homer”, 94) ); grades.insert ( pair<string,int>(“Marge”, 95) );
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
5/12/09 Map Member Functions Iterators: begin Return iterator to beginning of map end Return iterator to end of map rbegin Return reverse iterator to reverse beginning (end) rend Return reverse iterator to reverse end (beginning)
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Capacity: empty Test whether map is empty or not size Return container size of map max_size Return maximum size of map Modifiers: insert Insert element erase Erase element 5/12/09 Multimap Whats the difference between map and multimap? Not much. Multimaps are like maps but allow duplicate keys. Example: #include &lt;map&gt; multimap&lt;string, int&gt; grades; grades.insert ( pair&lt;string,int&gt;(Homer,94) ); grades.insert ( pair&lt;string,int&gt;(Marge,95) ); grades.insert ( pair&lt;string,int&gt;(Homer,50) ); 5/12/09 Output the map or multimap Code: map&lt;string,int&gt;::iterator it; for( it = grades.begin(); it != grades.end(); it++ ) { cout &lt;&lt; &quot;Name: &quot; &lt;&lt; it-&gt;first &lt;&lt; &quot; Grade: &quot; &lt;&lt; it-&gt;second &lt;&lt; endl; } Output: Name: Homer Grade: 94 5/12/09 The End Sources: www.cplusplus.com www.cppreference.com www.sgi.com/tech/stl/...
View Full Document

This note was uploaded on 05/06/2008 for the course CSE 1341 taught by Professor Lapio during the Spring '07 term at SMU.

Page1 / 7

Maps n Multimaps - Capacity: empty Test whether map is...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online