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






#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