1: //set11.cpp
3: #include <iostream>
4: #include <string>
5: #include <set>
6: using namespace std;
8: int main()
9: {
10: cout << "\nThis program illustrates the insert() member function "
11: "for sets.";
12: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
14: cout << "\nFirst we create an array of name strings, and\nthen a set "
15: "of name strings from that array.";
16: string a[] = {"Tom", "Dick", "Harry", "Alice"};
17: set<string> s(a, a+4);
18: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
20: cout << "\nHere are the " << s.size() << " names in the set:\n";
21: set<string>::iterator p = s.begin();
22: while (p != s.end()) cout << *p++ << endl;
23: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
25: cout << "\nNow we try to insert Bob into the set.";
26: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
27: pair<set<string>::iterator, bool> insertResult;
28: insertResult = s.insert("Bob");
29: if (insertResult.second == true)
30: cout << "\nSuccess! " << *insertResult.first
31: << " is now in the set.";
32: else
33: cout << "\nRats! That insertion did not succeed."
34: "\nWhy not? Because " << *insertResult.first
35: << " was already in the set.";
36: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
38: cout << "\nNext we try to insert Dick into the set.";
39: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
40: insertResult = s.insert("Dick");
41: if (insertResult.second == true)
42: cout << "\nSuccess! " << *insertResult.first
43: << " is now in the set.";
44: else
45: cout << "\nRats! That insertion did not succeed."
46: "\nWhy not? Because " << *insertResult.first
47: << " was already in the set.";
48: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
50: cout << "\nNow we try to insert Sam, Carl and Jack, in that order.";
51: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
52: set<string>::iterator p1, p2, p3;
53: p1 = s.insert(s.begin(), "Sam");
54: p2 = s.insert(s.end(), "Carl");
55: p3 = s.insert(s.begin(), "Jack");
56: cout << "\nHere are the names in the set after those three "
57: "attempted insertions:\n";
58: p = s.begin();
59: while (p != s.end()) cout << *p++ << endl;
60: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
62: cout << "\nAnd the names pointed to by the returned iterators "
63: "\nof these three calls to insert() are:\n";
64: cout << *p1 << endl;
65: cout << *p2 << endl;
66: cout << *p3 << endl;
67: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
69: cout << "\nFinally we create another set containing some names that "
70: "are already in the\nset, and some that are not. Then we try to "
71: "insert all of the names from this\nnew set into the previous set "
72: "using the insert() function. Some of them will\ngo in, and some "
73: "won't, because they are already there.";
74: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
76: string a2[] = {"John", "Amy", "Tom", "Harry", "Fred", "Dan"};
77: set<string> otherSet(a2, a2+6);
79: cout << "\nHere are the names in the new set that we try to insert "
80: "into the old set:\n";
81: p = otherSet.begin();
82: while (p != otherSet.end()) cout << *p++ << endl;
83: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
85: s.insert(otherSet.begin(), otherSet.end());
86: cout << "\nHere are the names in the set after these "
87: "attempted insertions:\n";
88: p = s.begin();
89: while (p != s.end()) cout << *p++ << endl;
90: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
91: }