uses a multimap to store names and phone numbers. : multimap insert « map multimap « C++ Tutorial

Home
C++ Tutorial
1.Language Basics
2.Data Types
3.Operators statements
4.Array
5.Development
6.Exceptions
7.Function
8.Structure
9.Class
10.Operator Overloading
11.Pointer
12.File Stream
13.template
14.STL Introduction
15.string
16.vector
17.list
18.bitset
19.set multiset
20.valarray
21.queue stack
22.deque
23.map multimap
24.STL Algorithms Modifying sequence operations
25.STL Algorithms Non modifying sequence operations
26.STL Algorithms Binary search
27.STL Algorithms Sorting
28.STL Algorithms Merge
29.STL Algorithms Min Max
30.STL Algorithms Iterator
31.STL Algorithms Heap
32.STL Algorithms Helper
C++ Tutorial » map multimap » multimap insert 




#include <iostream>
#include <map>
#include <string>
using namespace std;

void shownumbers(const char *n, multimap<string, string> mp);

int main()
{
  multimap<string, string> phonemap;

  // Insert elements by using operator[].
  phonemap.insert(pair<string, string>("A""B: 555-1111"));
  phonemap.insert(pair<string, string>("C""D: 555-1234"));
  phonemap.insert(pair<string, string>("E""F: 555-1122"));

  phonemap.insert(pair<string, string>("J""K: 314 555-2222"));
  phonemap.insert(pair<string, string>("L""M: 314 555-3322"));

  phonemap.insert(pair<string, string>("O""P: 666 555-9876"));
  phonemap.insert(pair<string, string>("Q""R: 555 555-1010"));
  phonemap.insert(pair<string, string>("S""T: 444 555-9999"));

  shownumbers("A", phonemap);
  shownumbers("C", phonemap);
  shownumbers("E", phonemap);

  int count = phonemap.erase("A");
  cout << count << " elements have been removed.\n\n";

  shownumbers("Ken", phonemap);

  return 0;
}

// Show all numbers for a given name.
void shownumbers(const char *n, multimap<string, string> mmp) {
  multimap<string, string>::iterator itr;

  // Find the first matching key.
  itr = mmp.find(n);

  if(itr != mmp.end()) {
    cout << "the numbers for " << n << ": " << endl;
    do {
      cout << " " << itr->second << endl;
    ++itr;
  while (itr != mmp.upper_bound(n));
}
else
  cout << "No entry for " << n << " found." << endl;
}














23.13.multimap insert
23.13.1.uses a multimap to store names and phone numbers.
23.13.2.Insert into multimap with key-value pair
23.13.3.Insert into multimap with make-pair function
23.13.4.Insert value pair to int, double multimap
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.