C++STL common operations of multiset articles
Introduction:
Multiset and set are roughly the same, both are sets.
For detailed introduction of set, please see: https://blog.csdn.net/qq_45985728/article/details/112791689
The difference between multiset and set is that multiset allows duplicate elements.
multiset<int> multi;
multi.insert(2);
multi.insert(3);
multi.insert(2);
multi.insert(1);
multi.insert(2);
for (auto it = multi.begin(); it != multi.end(); ++it)
cout << *it << endl;
We can see that in the multiset, duplicate elements are indeed allowed.
multiset<int> multi;
multi.insert(2);
multi.insert(3);
multi.insert(2);
multi.insert(1);
multi.insert(2);
multi.erase(2);
for (auto it = multi.begin(); it != multi.end(); ++it)
cout << *it << endl;
For the specified key value, the erase function in multiset deletes all elements equal to the key value. In the above program, the original state is 1, 2, 2, 2, 3, but after only one sentence of erase(2) is executed, the remaining elements are only 1, 3.
multiset<int> multi;
multi.insert(2);
multi.insert(2);
cout << multi.count(2);
The output of the program is 2
As we mentioned in the previous set article, the return value of count can only be 1 or 0 (existing or non-existing). Since multiset allows duplicate elements to appear, the return value of count may be greater than 1.
multiset
If you find any problems, please correct me!