Set Features:
- All elements are not duplicated, and repeated insertions of existing new values are not valid;
- All elements are arranged in order; except Unordered_set
- The keys and values are the same, so the values in the set are immutable
The list of member functions for the set is as follows:
1.begin ()--Returns an iterator that points to the first element//If the current container is empty, you cannot dereference (dereferenced) The iterator value returned.
2. Clear ()--Clears all elements
3. Count ()--Returns the number of elements of a value//Set.count (x)--Returns the number of X elements, note: The result of count here can only be 0 (NO) or 1 (with and only one ) because the elements within the set cannot be duplicated.
4. Empty ()--if the collection is empty, returns TRUE//before the iterator needs to verify whether it is empty
5. End ()--Returns an iterator pointing to the last element
6. Equal_range ()--returns two iterators in the set with the upper and lower bounds equal to the given value
7. Erase ()--delete the elements in the collection
8. Find ()--Returns an iterator to the element being found//set.find (x)--An iterator that returns an X element, that is, a pointer to that node; return Set.end () not found
9. Get_allocator ()--the allocator that returns the collection
Insert ()--Inserts an element in the collection//There is a function similar to it: Emplace. The difference between: When adding a new element, the constructor is called differently.
Lower_bound ()--Returns an iterator that points to the first element greater than (or equal to) a value
Key_comp ()--Returns a function for comparing values between elements
Max_size ()--Returns the maximum limit of the elements that the set can hold
Rbegin ()--Returns a reverse iterator pointing to the last element in the collection
Rend ()--Returns a reverse iterator pointing to the first element in the collection
Size ()--number of elements in the collection
+ Swap ()--swap two set variables
Upper_bound ()--Returns an iterator that is larger than a value element
Value_comp ()--Returns a function to compare the values between elements
Unlike vectors and lists, set and map are associative containers. The set interior is based on red-black trees . Insert and delete operations are more efficient because only the relevant pointers need to be modified without moving the data.
Will the iterator fail after the data delete operation? When you delete set data, the actual action is to delete a node in the red-black tree, and then the relevant pointers are adjusted. Iterators that point to other elements still point to the original location and do not change, so the other iterators are not invalidated after a node is deleted. The same is true for list and map. However, by removing an element from the vector, the other iterators in the vector are invalidated because the vector is an array-based one, and after the element is deleted, the subsequent elements move forward, so the iterator to the subsequent element is invalidated.
Just a little bit more about the implementation of the iterator. An iterator is an object in which avector iterator encapsulates an array subscript, and alist, map, and set iterator is a pointer that encapsulates an element node.
Also, from a mathematical level, set a set, like a bag containing a lot of small balls. But the red-black tree is a special two-fork search tree, and the elements in the set have specific positions in the red and black trees according to their values, and are not movable. So, 1 is the search operation is very efficient O (log n), 2 is set in the value of the element is immutable.
Reference article:
Http://classfoo.com/ccby/article/w2pUE
45771597
Https://www.cnblogs.com/omelet/p/6627667.html
53741372
Set in C + +