Set in C + +

Source: Internet
Author: User

Set Features:

    1. All elements are not duplicated, and repeated insertions of existing new values are not valid;
    2. All elements are arranged in order; except Unordered_set
    3. 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 + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.