Online instance [cpp] # include <string> # include <set> # include <conio. h> # include <iostream> using namespace std; // The difference between multiple sets of multiset and set: multiset can have multiple identical elements, and the elements in set are unique. Therefore, different functions: When the erase () parameter is an element, all the same data is deleted. The usage of other functions is exactly the same as that of set. /* The internal structure of set and multiset is usually achieved by a balanced binary tree. When an element is placed in a container, it is automatically sorted by certain sorting rules. By default, the elements are sorted by less <> sorting rules. This automatic sorting feature accelerates the element search process, but also brings about a problem. You cannot directly modify the element values in the set or multiset container, this may violate the automatic sorting rules of elements. If you want to modify the value of an element, you must first Delete the original element and then insert a new element. A set is a container. The values of the elements contained in a set are unique. The difference between multiple sets in a set is that set supports unique key values. The values in a set are specific and only appear once. The duplicate key can appear in multiset and the same value can appear multiple times. * // Definition and initialization; no return value, or void test0 () {multiset <int> f; int myint [] = {, 6, 0 }; multiset <int> se (myint, myint + 5); // initialize [beg, end) multiset <int> th (se. begin (), se. end (); // initialize [beg, end), similar to the above multiset <int> fo (se); // copy constructor} // Add Delete elements; the return value is void, and the return value except erase () is void test1 () {// insert: 1. insert a data directly. insert a data entry at the iterator position. insert [beg, end) a piece of data multiset <int> myset; multiset <int >:: iterator it = myset. begin (); myset. Insert (20); for (int I = 0; I <5; I ++) {myset. insert (I * 10); // 0 10 20 30 40} myset. insert (it, 24); int myint [] = {5, 10, 15}; myset. insert (myint, myint + 3); // insert [beg, end); for (it = myset. begin (); it! = Myset. end (); it ++) {cout <* it <""; // 0 5 10 15 20 24 30 40} cout <endl; // erase: 1. delete A data 2. delete data at the iterator position 3. delete the data myset in the [beg, end) interval. erase (20); // 0 5 10 15 24 30 40 // delete all elements whose values are 20 it = myset. find (10); myset. erase (it); // 0 5 10 15 24 30 40 // Delete only one element it = myset. find (24); myset. erase (it, myset. end (); // 0 5 10 15 for (it = myset. begin (); it! = Myset. end (); it ++) {cout <* it <""; // 0 5 10 15} myset. clear (); // delete all elements // set attributes (usually those functions without parameters): empty (), size () if (myset. empty () // determines whether the set is null {cout <"\ nmyset is empty" <endl;} if (myset. size () = 0) // determines whether the set is null {cout <"myset size is 0" <endl;} int myints [] = {24, 12, 12, 18, 9}; multiset <int> first (myints, myints + 2), second (myints + 1, myints + 5); // The Initialization is: [beg, end) first. swap (second); for (it = first. begin (); it! = First. end (); it ++) {cout <* it <""; // 9 12 18} cout <endl; for (it = second. begin (); it! = Second. end (); it ++) {cout <* it <""; // 12 24} cout <endl ;}// access element; the returned value is Type T void test2 () {int myint [] = {, 23,}; multiset <int> myset (myint, myint + 5 ); cout <* myset. find (65) <endl; // 65 // locate the iterator element. If this element is not found, myset is returned. end () (next position of the last element) cout <myset. count (65) <endl; // 2 query the number of elements} // return iterator; Return Value: iterator, or reverse_iterator void test3 () {int myint [] = {,}; multiset <int> myset (Myint, myint + 5); // initialization: [beg, end) for (multiset <int >:: iterator it = myset. begin (); it! = Myset. end (); it ++) {cout <* it <""; // 13 23 42 65 65 75} cout <"\ nreverse myset: "<endl; for (multiset <int>: reverse_iterator it = myset. rbegin (); it! = Myset. rend (); it ++) {cout <* it <""; // 75 65 42 23 13} cout <endl ;} // other void test4 () {int myint [] = {,}; multiset <int> myset (myint, myint + 6); // initialization: [beg, end) multiset <int>: key_compare com = myset. key_comp (); multiset <int >:: iterator it = myset. begin (); int keyHighest = * myset. rbegin (); // * myset. end () do {cout <"" <* it; // 13 23 42 65 65 75} while (com (* (it ++), keyHighest )); cout <en Dl; multiset <int >:: value_compare val = myset. value_comp (); int valHighest = * myset. rbegin (); it = myset. begin (); do {cout <"" <* it; // 13 23 42 65 65 75} while (val (* (it ++), valHighest )); cout <endl; multiset <int>: iterator itlows, itups; itlows = myset. lower_bound (65); itups = myset. upper_bound (65); cout <* itlows <"" <* itups <endl; // 65 75 std: pair <multiset <int>: iterator, multiset <int >:: iterator> pa = myset. Optional _range (42); cout <"the lower bound point to:" <* pa. first <endl; // 42 cout <"the upper bound point to:" <* pa. second <endl; // 65 // pai_range (): returns pair (iterator, iterator). The first iterator in pair is the iterator returned by lower_bound, the second iterator in pair is the iterator returned by upper_bound (). If the two iterators are equal, this keyword does not exist in map; it is in an ordered array, take the previous Q [k-1] value of a value Q [k] or the former Q [k + 1] and the latter Q [k + 1] // lower_bound (): the return value is iterator, and the lower bound of the keyword to be searched (an iterator) is returned, pointing to the first element of the key value <= key // upper_bound (): the return value is I Returns the upper bound (an iterator) of the keyword to be searched, pointing to the first element of the key value> key} // traverses the data void test5 () {// 1 in the map. use the forward iterator // 2. use the opposite iterator int myint [] = {, 23,}; multiset <int> myset (myint, myint + 6); // initialization: [beg, end) for (multiset <int >:: iterator it = myset. begin (); it! = Myset. end (); it ++) {cout <* it <""; // 13 23 42 65 65 75} cout <"\ nreverse myset: "<endl; for (multiset <int>: reverse_iterator it = myset. rbegin (); it! = Myset. rend (); it ++) {cout <* it <""; // 75 65 42 23 13} cout <endl;} void Test (char h) {cout <"press key ="