map and multimap
Download
Skip this Video
Download Presentation
Map and Multimap

Loading in 2 Seconds...

play fullscreen
1 / 10

Map and Multimap - PowerPoint PPT Presentation


  • 66 Views
  • Uploaded on

Map and Multimap. ITK 169 Fall 2003. Map. A Standard Templated Library #include <map> Each value of a map is a pair. The pair is made up of a key and an element. The key may be of any type and it is what determines the ordering of the map.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Map and Multimap' - lilka


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
map and multimap

Map and Multimap

ITK 169

Fall 2003

slide2
Map
  • A Standard Templated Library
  • #include
  • Each value of a map is a pair.
  • The pair is made up of a key and an element.
  • The key may be of any type and it is what determines the ordering of the map.
  • The element is the type of “things” stored in the map.
the key
The key
  • In program 5, the key was a data member of the Player object and the object was never actually stored in the map.
    • map playerIndex;
  • Here, we did not have a map of Players – rather we had a map of indexes.
    • The integer stored as the second element of the pair was the record number or location of this player in the file.
      • This is one way a map could be used.
program 6
Program 6
  • In program 6 we might have decided to store our collection of Person pointers in a map instead of a list.

map mymap;

  • Here name would have been a complete name – last first – not stored individually as in the actual object.
  • This would have automatically sorted the collection by name.
map elements are unique
map Elements are Unique
  • In a map, the key must be a unique element.
    • Therefore if you have a map of students and you make their grade point the key, each student’s grade point would have to be unique (which is not likely to happen).
miltimap
miltimap
  • A multimap is a map that allows elements with duplicate keys.
  • Still - #include
map methods
map Methods
  • As with the other STL classes, the expected methods are available:
      • Constructors
      • size
      • empty
      • insert
      • erase
      • find
      • begin
      • end
pairs
pairs
  • The insert method takes a value that is a pair as its argument
  • A pair is defined

pair name(first, second);

  • So if we have a map defined as:

map students;

  • And we have a pair defined as:

pair aStu(“Smith, Bob”, 3.96);

  • We could insert the pairs using:

students.insert(aStu);

associative array
Associative Array
  • In typical arrays, the index is always an integer.
  • In associative arrays, the index can be any type – int, double, string, or some user defined object
  • An assignment like

students[“Brown, Mary”] = 3.5;

  • Inserts the pair <“Brown, Mary”, 3.5> into the students map.
ad