Detailed explanation of the collection set of C++ STL

Part.I Attention

insert image description here

setPoints to note when using :

  • add citation#include <set>
  • Different from mapcontainers multimap, seteach key-value pair stored in the container requires that the key keyand value valuemust be equal.
  • The elements in the associative container set(collection) are unique, and the system will automatically sort them according to their values.
  • The standard associative container in C++ STL set, multiset, map, multimapuses a very efficient balanced retrieval binary tree: red-black tree, also known as RB tree (Red-Black Tree).

Official website: https://cplusplus.com/reference/set/

Part.II Function

Chap.I set functions in the header file

The commonly used functions in set are shown in the figure below

insert image description here
A table is listed below, explaining the meaning of these functions.

function meaning
begin() Returns a bidirectional iterator pointing to the first (note, the sorted first) element in the container. If the set container is const-qualified, this method returns a bidirectional iterator of const type.
end() Returns a bidirectional iterator pointing to the position after the last element of the container (note that it is the last one that has been sorted) , usually begin()used in conjunction with . If the set container is const-qualified, this method returns a bidirectional iterator of const type.
rbegin() Returns a reverse bidirectional iterator pointing to the last (note, the last sorted) element. If the set container is const-qualified, this method returns a reverse bidirectional iterator of const type.
rend() Returns a reverse bidirectional iterator pointing to the position preceding the position of the first (note, the first sorted) element. If the set container is const-qualified, this method returns a reverse bidirectional iterator of const type.
cbegin() It begin()has the same function as , except that the const attribute is added on top of it, which cannot be used to modify the element value stored in the container.
cend() It end()has the same function as , except that the const attribute is added on top of it, which cannot be used to modify the element value stored in the container.
crbegin() It rbegin()has the same function as , except that the const attribute is added on top of it, which cannot be used to modify the element value stored in the container.
crend() It rend()has the same function as , except that the const attribute is added on top of it, which cannot be used to modify the element value stored in the container.
find(val) Look for the element whose value is val in the set container, if found successfully, return the bidirectional iterator pointing to the element; otherwise, return the same end()iterator as the method. In addition, if the set container is const-qualified, this method returns a bidirectional iterator of const type.
lower_bound(val) Returns a bidirectional iterator pointing to the first element greater than or equal to val in the current set container. If the set container is const-qualified, this method returns a bidirectional iterator of const type.
upper_bound(val) Returns an iterator pointing to the first element greater than val in the current set container. If the set container is const-qualified, this method returns a bidirectional iterator of const type.
equal_range(val) This method returns a pair object (contains two bidirectional iterators), where the return value of pair.firstthe and method are equivalent, and the return value of the method is equivalent. That is, the method will return a range that contains elements with the value val (each element in the set container is unique, so the range contains at most one element).lower_bound()pair.secondupper_bound()
empty() Returns true if the container is empty; otherwise, false.
size() Returns the number of elements stored in the current set container.
max_size() Returns the maximum number of elements that the set container can hold. Different operating systems have different return values.
insert() Insert elements into the set container.
erase() Deletes the elements stored in the set container.
swap() Swaps all elements stored in 2 set containers. This means that the 2 set containers operated on must be of the same type.
clear() Clear all the elements in the set container, that is, the set container size()is 0.
emplace() Constructs a new element directly at the specified position in the current set container. Its effect is the same as insert(), but more efficient.
emplace_hint() In essence, emplace()it is the same as the method of constructing new elements in the set container, the difference is that the user must provide this method with an iterator indicating the position where the new element is generated as the first parameter of the method.
count(val) In the current set container, find the number of elements whose value is val, and return it. Note that since the value of each element in the set container is unique, the maximum return value of this function is 1.

Functions in the Chap.II algorithm

In the header file #include <algorithm>, there are operations related to sets such as intersection, union, difference, etc.

  • set_intersection: take set intersection
  • set_union: fetch and union
  • set_difference: take set difference
  • set_symmetric_difference: Take the set symmetric difference

The usage of these functions is almost the same. They all have 5 parameters. The meaning of the first 4 parameters is the same. There are two types of the fifth parameter. Take this as an example set_union():

  • set_union(A.begin(),A.end(),B.begin(),B.end(),inserter(C1 , C1.begin()));
  • set_union(A.begin(),A.end(),B.begin(),B.end(),ostream_iterator(cout," “));

The first two parameters of the first four parameters are the sum Aof begin()sets end(); the third and fourth parameters are the sum Bof begin()sets end(); the meaning of the first usage is to store the result of the union C1in the set, and the meaning of the second usage is to store Find the output of the union result.


In addition, about setand vectormutual conversion can be performed as follows:

vector<int> v;
v = {
    
    1,2,2,3,3,4};					//建立vector
set<int> st(v.begin(), v.end());	//在构造函数中可以直接实现vector转set
v.assign(st.begin(), st.end());		//用assign实现set转vector

Part.III Code

Problem 2032 in LeetCode . Values ​​that appear in at least two arrays are very suitable setfor this. Here is C++the solution:

class Solution {
    
    
public:
    vector<int> twoOutOfThree(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3) {
    
    
        set<int> s1(nums1.begin(),nums1.end()), s2(nums2.begin(),nums2.end()), s3(nums3.begin(),nums3.end()), s12,s13,s23;
        set_intersection(s1.begin(),s1.end(),s2.begin(),s2.end(),inserter(s12, s12.begin() ));
        set_intersection(s1.begin(),s1.end(),s3.begin(),s3.end(),inserter(s12, s12.end() ));
        set_intersection(s2.begin(),s2.end(),s3.begin(),s3.end(),inserter(s12, s12.end() ));
        vector<int> ans;
        ans.assign(s12.begin(), s12.end());
        return ans;
    }
};

Guess you like

Origin blog.csdn.net/Gou_Hailong/article/details/128398866