Article directory
1. mulset container
1. Introduction to std::multiset container
In the Standard Template Library (STL, Standard Template Library) of the C++ language, the std::multiset container is provided.
- The key values of elements in this container can be repeated;
- The elements in this container are ordered and sorted according to the specified rules;
When inserting an element into the std::multiset container , there is no need to verify whether the element already exists in the collection. It is directly inserted into the specified position according to the sorting rules;
The std::multiset container does not support inserting elements at the specified position;
The std::multiset container also does not support direct access to elements using subscript positions;
Before using the std::multiset container, you need to import the set header file;
#include "set"
Containers similar to set containers include multiset containers. The only difference is that elements in a set can only appear once, while elements in a multiset can appear multiple times;
The elements in the std::multiset container cannot be modified directly, the original elements can only be deleted first, and then new elements can be inserted;
2. Code example - multiset container
In the following code, a multiset container is created to store repeated elements;
multiset<int> myMultiSet = {
1, 2, 3, 4, 5, 4, 3, 2, 1 };
When the above container is initialized, the elements in the container will be automatically sorted. The sorted order is as follows:
1 1 2 2 3 3 4 4 5
Code example:
#include "iostream"
using namespace std;
#include "set"
int main() {
// 初始化 multiset 容器
multiset<int> myMultiSet = {
1, 2, 3, 4, 5, 4, 3, 2, 1 };
// 遍历打印 multiset 中的所有元素
for (auto& elem : myMultiSet) {
cout << elem << " ";
}
cout << endl;
// 控制台暂停 , 按任意键继续向后执行
system("pause");
return 0;
};
Results of the :
1 1 2 2 3 3 4 4 5
请按任意键继续. . .
2. Introduction to the common operation API of std::multiset container
1. Introduction to commonly used APIs
Common operations of std::multiset containers: The interfaces of std::multiset containers and std::set containers are basically the same;
- insert(): Insert one or more elements into the multiset container;
- erase(): delete one or more elements in the multiset container;
- clear(): Clear all elements in the multiset container;
- find(): Find a specific element in the multiset container;
- count(): Returns the number of specific elements in the multiset container . This function can only return 0 or 1 in the set container, which is meaningful in the multiset container;
- lower_bound(): Returns the range of elements in the multiset container that are greater than or equal to the given key value;
- upper_bound(): Returns the range of elements in the multiset container that are greater than the given key value;
- equal_range(): Returns the range of elements in the multiset container that are equal to the given key value;
2. Code examples - common operations of multiset containers
Code example:
#include "iostream"
using namespace std;
#include "set"
void printMS(multiset<int>& ms) {
// 遍历打印 multiset 中的所有元素
for (auto& elem : ms) {
cout << elem << " ";
}
cout << endl;
}
int main() {
// 初始化 multiset 容器
multiset<int> myMultiSet = {
1, 2, 3, 4, 5, 4, 3, 2, 1 };
// 遍历打印容器
printMS(myMultiSet);
// 插入元素
myMultiSet.insert(9);
// 遍历打印容器
cout << "插入元素 9 : ";
printMS(myMultiSet);
// 删除元素
myMultiSet.erase(3);
// 遍历打印容器
cout << "删除元素 3 : ";
printMS(myMultiSet);
// 获取元素数量
int count = myMultiSet.count(2);
// 遍历打印容器
cout << "元素 2 个数 : " << count << endl;
printMS(myMultiSet);
// 清空元素
myMultiSet.clear();
// 遍历打印容器
cout << "清空元素 : ";
printMS(myMultiSet);
// 控制台暂停 , 按任意键继续向后执行
system("pause");
return 0;
};
Results of the :
1 1 2 2 3 3 4 4 5
Insert element 9: 1 1 2 2 3 3 4 4 5 9 Delete
element 3: 1 1 2 2 4 4 5 9
Number of elements 2: 2
1 1 2 2 4 4 5 9
Clear Element:
Please press any key to continue. . . .