1: //map16.cpp
3: #include <iostream>
4: #include <map>
5: #include <utility>
6: using namespace std;
8: int main()
9: {
10: cout << "\nThis program illustrates the swap() member function "
11: "of the map interface.";
12: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
14: //Create an array of pairs
15: pair<char, int> a[] =
16: {
17: pair<char, int>('A', 65),
18: pair<char, int>('B', 66),
19: pair<char, int>('C', 67),
20: pair<char, int>('D', 68),
21: pair<char, int>('E', 69),
22: pair<char, int>('F', 70),
23: pair<char, int>('G', 71),
24: pair<char, int>('H', 72),
25: pair<char, int>('I', 73),
26: pair<char, int>('J', 74)
27: };
28: map<char, int> m1(a, a+3);
29: map<char, int> m2(a+3, a+10);
31: cout << "\nWe begin by creating two maps.";
32: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
34: cout << "\nThe first map has size " << m1.size() << " and contains the "
35: "following pairs:\n";
36: map<char, int>::iterator p = m1.begin();
37: while (p != m1.end())
38: {
39: cout << p->first << " " << p->second << endl;
40: ++p;
41: }
42: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
44: cout << "\nThe second map has size " << m2.size() << " and contains the "
45: "following pairs:\n";
46: p = m2.begin();
47: while (p != m2.end())
48: {
49: cout << p->first << " " << p->second << endl;
50: ++p;
51: }
52: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
54: cout << "\nNow we swap the two maps using the swap() function "
55: "from the map interface.";
56: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
58: m1.swap(m2);
60: cout << "\nAfter the swap the first map has size " << m1.size()
61: << " and contains the following pairs:\n";
62: p = m1.begin();
63: while (p != m1.end())
64: {
65: cout << p->first << " " << p->second << endl;
66: ++p;
67: }
68: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
70: cout << "\nAnd the second map has size " << m2.size() << " and contains the "
71: "following pairs:\n";
72: p = m2.begin();
73: while (p != m2.end())
74: {
75: cout << p->first << " " << p->second << endl;
76: ++p;
77: }
78: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
79: }