Article directory
foreword
Since this article involves a lot of content, in order to be as detailed as possible, various examples will be attached, so there will be more code samples, and for a better layout, this series will be explained separately, and in the early stage for better fit For actual combat needs, only some commonly used STL containers and basic functions will be explained for the time being.
In order to make each article have a certain degree of independence, most of the subsequent articles will extract the knowledge points involved in the previous articles, and the corresponding examples will be added in the follow-up.
The STL series of knowledge in this article refers to "C++ Language Program Involvement (5th Edition)"
STL Guide
- Basic functions and classification of containers
- sequence container
- associative container
map
A map element type is a 2-tuple consisting of a key and additional data
special function
1. make_pair(v1,v2)
Construct a binary group through the make_pair function below, and the following is the function template of the function
template<class T1,class T2>
pair<T1,T2>make_pair(T1 v1,T2 v2){
return pair<T1,T2>(v1,v2);
}
2. erase(keyName)
This method of deleting an element through a key is to find the element according to this key, and it is more efficient to delete an element through an iterator, which can directly locate the element.
3. [ ]
map overloads the [] operator, you can insert new elements, modify or query additional data of existing elements through []
Strictly speaking, the operation performed by s[k] is to query the element whose key is k in s, if it exists, return the reference of its additional data, if it does not exist, insert a new element into s and return A reference to the additional data of this element. The initial value of the additional data is V(), where V is the type of the additional data.
If V is a numeric type or a pointer type, the value of V() is 0
Although the [] operator is convenient to use, it cannot completely replace functions such as insert and find, because it will automatically create new elements for new creation, so it is impossible to determine whether there is an element with the specified key in the container.
basic skills
q1, q2 represent the input iterator that does not point to the s element
S represents the container type name
s represents the instance of S
T represents the element type of the S container
t represents the instance of T
K represents the key type of S
k represents the instance of K
n represents an integer
p1 , p2 represents an iterator pointing to s
Constructor
In addition to using the default constructor, you can also use the sequence represented by the iterator range to construct
1.S s(q1,q2);
Use the data in the interval [q1,q2) as the element of s to construct s
single associative container
When elements with the same key are present in the range [q1,q2), only the first element will be added to s
multiple associative containers
All elements in the range [q1,q2) are unconditionally added to s
insert element
Insert one or more elements
Unlike the sequential container, there is no need to specify the insertion position through the iterator
1.s.insert(t)
insert t into s
single associative container
The insertion is successful only when there is no element with the same key, return pair<S::iterator, bool>
When the insertion is successful, return the iterator of the inserted element and true,
otherwise return the iterator and the iterator of the element with the same key as t false
multiple associative containers
2.s.insert(p1,t)
Insert t into s, p1 is the insertion position of a prompt, if the prompt is accurate (that is, the key size of t is just before p1), the insertion efficiency can be improved, and the prompt can be correct if the prompt is inaccurate, and an iterator is always returned
single associative container
Only when there is no element with the same key can it be successfully inserted
After the insertion is successful, return the iterator of the inserted element
Otherwise returns an iterator over the elements with the same key as t
3.insert(q1,q2)
It is equivalent to executing s.insert(x) for each element x in the interval [q1,q2) in sequence
element deletion
To delete through erase, the two call forms provided by the sequence container to specify the deletion of elements through the iterator are still valid for the associative container, and the associative container also allows the deletion of elements by the key
1.s.erase(p1)
Delete the element pointed to by p1
2.s.erase(p1,p2)
Delete elements in the interval [p1,p2)
3.s.erase(k)
Delete all elements whose key is k, and return the number of deleted elements
Key-based lookup and counting
1.s.find(k)
Find any element whose key is k, return the iterator of the element, if there is no element with key k in s, return s.end()
2.s.lower_bound(k)
Get the iterator of the first element in s whose key value is not less than k
3.s.upper_bound(k)
Get the iterator of the first element in s whose key value is greater than k
4.s.equal_range(k)
Get an interval represented by pair<S::iterator, S::iterator>, denoted as [p1,p2), which just contains all elements whose key is k, p1==s.lower_bound(k) and p2= =s.upper_bound(k) must be established.
5.s.count(k)
Get the number of elements whose key is k in the s container
List initialization of associative containers
Similar to list initialization for sequential containers
For unary associative containers, just provide a list of elements directly
For binary associative containers, it needs to be realized by means of {key, value} element key-value pairs
set<int>integer_set={
3,5,7};//以列表中int类型的元素为键创建一元集合对象
map<string,int>id_map={
{
"小明",1},{
"李华",2}};
Precautions
Insertion and deletion of associative containers does not invalidate any existing iterators, pointers, or references
Therefore, avoid using insert and delete operations during the iterator's traversal of the container, or need to stop the traversal after the operation