[STL] Multiset multiset and multimap multimap

14 Day Reading Challenge

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

multiset

Multiple collections allow duplicate elements. The usage of multiple collections is similar to that of collections. There are only slight differences in several member functions. The main difference is that the restriction that the key must be unique is removed.

multiple mapping

Multiple mapping allows one key to correspond to multiple mappings of additional data. Multiple mapping is similar to mapping usage, with only minor differences in several member functions. The main difference is that the restriction that the key must be unique is removed.

Precautions

Multiple associative containers generally use less find member functions, and more use equal_range and count member functions

Since a key may correspond to multiple elements, the position pointed to by the iterator obtained by using the find member function is uncertain. Generally, the find member function is only used when determining whether a key exists in the container.

To access each element corresponding to a key, use the equal_range member function.

To get the number of elements corresponding to a key, you can use the count member function

The [] used by the map is not supported in the multimap, because a key cannot correspond to a unique element

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

Guess you like

Origin blog.csdn.net/weixin_45720193/article/details/127603497