Cycle through the map in the reverse direction : map iterator « map multimap « C++ Tutorial






#include <iostream>
#include <string>
#include <map>
#include <utility>

using namespace std;

void show(const char *msg, map<string, string> mp);

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

  phonemap["A"] = "444-555-1234";
  phonemap["B"] = "555-555-6576";
  phonemap["C"] = "555-555-9843";

  show("Here is the original map: ", phonemap);

  phonemap["B"] = "555 555-5555";
  cout << "New number: " << phonemap["B"] << endl;

  // Cycle through the map in the reverse direction.
  map<string, string>::reverse_iterator ritr;
  cout << "Display phonemap in reverse order:" << endl;
  for(ritr = phonemap.rbegin(); ritr != phonemap.rend(); ++ritr)
     cout << ritr->first << ": " << ritr->second << endl;

  return 0;
}

// Display the contents of a map<string, string> by using an iterator.
void show(const char *msg, map<string, string> mp) {
  map<string, string>::iterator itr;

  cout << msg << endl;

  for(itr=mp.begin(); itr != mp.end(); ++itr)
   cout << " " << itr->first << ": " << itr->second << endl;

  cout << endl;
}








23.7.map iterator
23.7.1.Use iterator to loop through map and print all elements
23.7.2.Loop through map and print all the key/value pair
23.7.3.Display the first element in map
23.7.4.Display the last element in map
23.7.5.reverse_iterator from map
23.7.6.Cycle through the map in the reverse direction
23.7.7.Declare a reverse iterator to a map
23.7.8.Declare an iterator to a map
23.7.9.const_iterator for map
23.7.10.Create int string map and print all element pair