1: //multiset01.cpp 3: #include <iostream> 4: #include <set> 5: #include <string> 6: using namespace std; 8: int main() 9: { 10: cout << "\nThis program illustrates the essential difference between " 11: "sets and multisets.\nFirst we create an empty set of integers, " 12: "then we try to insert multiple copies\nof the same value into " 13: "that set. Only a single copy of each value goes into the\nset, " 14: "thus showing that the values stored in a set are unique."; 15: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 17: set<int> s; 18: pair<set<int>::iterator, bool> result; 20: cout << "\nWe being by trying to insert 1 twice and 2 once, each time " 21: "\nignoring the return value of the call to insert()."; 22: s.insert(1); 23: s.insert(1); 24: s.insert(2); 25: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 27: cout << "\nAfter this, the values in the set are: "; 28: set<int>::iterator ps = s.begin(); 29: while (ps != s.end()) cout << *ps++ << " "; 30: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 32: cout << "\nNext we try to insert 3, then 4 twice, and finally 5, " 33: "each time using\nthe return value of insert() to report on " 34: "the result."; 35: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 37: result = s.insert(3); 38: cout << "\n3 " << (result.second ? "" : "not ") << "inserted\n"; 39: result = s.insert(4); 40: cout << "4 " << (result.second ? "" : "not ") << "inserted\n"; 41: result = s.insert(4); 42: cout << "4 " << (result.second ? "" : "not ") << "inserted\n"; 43: result = s.insert(5); 44: cout << "5 " << (result.second ? "" : "not ") << "inserted\n"; 45: cout << "Press Enter to continue ... "; cin.ignore(80, '\n'); 47: cout << "\nAfter these attempts the set contents are: "; 48: ps = s.begin(); 49: while (ps != s.end()) cout << *ps++ << " "; 50: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 51: 52: cout << "\nNow we create an empty multiset of integers, and then try " 53: "to insert multiple\ncopies of the same value (two copies of 1, one" 54: "of 2 and 3, three of 4, and one\nof 5), and note that this time " 55: "all values are actually inserted, showing that\na multiset can " 56: "hold duplicat copies."; 57: multiset<int> multisetValues; 58: multisetValues.insert(1); 59: multisetValues.insert(1); 60: multisetValues.insert(2); 61: multisetValues.insert(3); 62: multisetValues.insert(4); 63: multisetValues.insert(4); 64: multisetValues.insert(4); 65: multisetValues.insert(5); 66: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 68: cout << "\nHere are the values in the mulitset: "; 69: multiset<int>::iterator pms = multisetValues.begin(); 70: while (pms != multisetValues.end()) cout << *pms++ << " "; 71: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 72: }