Detailed explanation of common usage of set

A set translates to a collection, which is a container that is automatically ordered internally and does not contain repeated elements . In the exam, there may be situations where it is necessary to remove duplicate elements, and it may be that these elements are relatively large or the type is not int, and the hash table cannot be directly opened. In this case, you can use set to retain the element itself without considering Its number. Of course, the situation mentioned above can also be solved by opening an array for subscript and element correspondence, but set provides a more intuitive interface, and can realize automatic sorting after adding set, so proficient use of set can do something Reduce the amount of thinking when doing some questions.

Required header files:

#include<set>

Other things needed:

using namespace std;

Definition of set

The format defined by set:

set<typename> name;

Its definition is basically the same as vector, or in fact, most STLs are defined in this way.
The typename here can still be any basic type, such as int, double, char, structure, etc.,
or STL standard container, such as vector, set, queue, etc.
As mentioned in the previous vector, if the typename is an STL container, remember to add a space between the >> symbol when defining it,
because some standard compilers before C++ 11 will treat it as a shift operation. Causes a compilation error.
Here are some simple examples:

Insert picture description here
The definition of set array is the same as that of vector:

set<typename> Arrayname[arraySize];

E.g:

set<int> a[100];

Thus, each of Arrayname[0] ~ Arrayname[ arraySize -1] is a set container.

Access to elements in the set container

Sets can only be accessed through iterators:

set<typename>:: iterator it;

typename is the type filled in when defining set. The following is an example where typename is int and char:

set<int>::iterator it;
set<char>::iterator it;

In this way, you get the iterator it, and you can access the elements in the set through *it.
Since STL containers other than vector and string do not support the * (it + i) access method,
Therefore, it can only be enumerated as follows: It
Insert picture description here
can be found that the elements in the set are automatically sorted in ascending order, and duplicate elements are automatically removed

Set common function example analysis

(1) insert()
insert(x) can insert x into the set container, and automatically increase sorting and de-duplication. The time complexity is O(logN), where N is the number of elements in the set
Insert picture description here
(2)find()
find(value) returns the iterator with the corresponding value in the set, and the time complexity is O(logN), where N is the number of elements in the set.
Insert picture description here
(3) erase()
There are two methods for erase(): delete a single element, delete all elements in an interval. ①Delete
a single element
st.erase(it) , it is the iterator to delete the element. The time complexity is O(1). It can be used in combination with find(). The
case is as follows: Insert picture description herest.erase(value) , where value is the value of the element to be deleted. The time complexity is O(logN), and N is the number of elements in the set.
Examples are as follows: ②Delete
Insert picture description here
all elements in an interval
st.erase(first,last) can delete all elements in an interval, where first is the starting iterator of the interval to be deleted, and last is the interval that needs to be deleted The next address of the end iterator, that is, delete all elements in [first, last). The time complexity is O (last-first).

Insert picture description here
(4) size()
size() is used to obtain the number of elements in the set, and the time complexity is O(1).
Insert picture description here
(5) clear()
Clear() is used to clear all the elements in the set. The complexity is O(N), where N is the number of elements in the set.
Insert picture description here

Common uses of set

The main function of set is to automatically remove duplicates and sort them in ascending order . Therefore, if you need to remove duplicates but it is not convenient to open the array directly, you can try to solve it with set.
Extension: The elements in the set are unique. If you need to deal with non-unique situations, you need to use multiset. In addition, the C++11 standard also adds unordered_set,
which replaces the red-black tree (Red Black Tree, a self-balancing binary search tree) implementation inside the set with a hash, so that it can be used to deal with the needs of only deduplication but not sorting. , The speed is much faster than set, interested readers can understand by themselves, not much explanation here.

Guess you like

Origin blog.csdn.net/qq_46527915/article/details/115139939