Article directory
1. set collection container
1. Introduction to set collection container
The set container in the STL container in the C++ language is a "set container".
- Each element in the container is "unique",
- And the elements in the collection container are arranged in a certain order;
When inserting an element into the set collection container , it will first verify whether the element already exists in the collection, and then insert it into the specified position according to the sorting rules;
The set collection container does not support inserting elements into the specified position;
The set collection container also does not support direct access to elements using subscript positions;
Before using the set collection 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 set collection container cannot be modified directly, the original elements can only be deleted first, and then new elements can be inserted;
2. Time complexity of set collection container operation
The bottom layer of the set collection container is implemented using the "red-black tree" data structure. The red-black tree is a "balanced binary tree", which is characterized by insertion/deletion operations, which are faster than linear tables;
The time complexity of the set collection container operation is the time complexity of the red-black tree operation;
A red-black tree is a self-balancing binary search tree, and the time complexity of its insertion and deletion operations can depend on the specific implementation and type of operation;
Insertion/deletion operations in red-black trees are divided into two situations:
- Under average circumstances: the time complexity of insertion/deletion operations in red-black trees is O(log n);
- In the worst case: the time complexity of the insertion/deletion operation of the red-black tree is O(n), all nodes need to be traversed, and the probability of occurrence is small;
n in the above time complexity refers to the number of element nodes in the red-black tree;
Comparing with the red-black tree, if the insertion/deletion operation is performed in the linear table, the time complexity is O(n). Obviously, the insertion/deletion operation performance of the red-black tree/set collection container is higher;
3. Common operations of set collection containers
set Common operations for collection containers:
- Insert elements: Call the insert function to insert elements into the set collection container. If the element already exists, no operation will be performed;
- Delete elements: Call the erase function to delete the specified element in the set collection container. If the element does not exist, no operation will be performed;
- Traverse the container: Use the set<T>::iterator iterator to traverse the set collection container;
- Find elements: call the find function to find whether the element exists in the set; if the element exists, this method returns an iterator pointing to the element; if the element does not exist, returns an iterator pointing to the end of the set;
2. Code example - set collection container
1. Code examples
#include "iostream"
using namespace std;
#include "set"
int main() {
// set 集合容器
set<int> se;
// 向容器中插入元素
se.insert(3);
se.insert(1);
se.insert(2);
// 遍历 set 集合容器
for (set<int>::iterator it = se.begin(); it != se.end(); it++)
{
cout << *it << " ";
}
// 回车换行
cout << endl;
// 控制台暂停 , 按任意键继续向后执行
system("pause");
return 0;
};
2. Execution results
Results of the :
1 2 3
Press any key to continue . . .