C + + Set learning Notes

Source: Internet
Author: User

stl~ (Multi) Set

Set Collection container: realizes the red black tree's balanced binary search tree data structure, when inserting the element, it automatically adjusts the binary tree arrangement, puts the element in the appropriate position, guarantees each sub-tree root node key value is bigger than the left subtree all node's key value, is less than the right subtree all node's key value; In addition, Also ensure that the height of the left subtree of the root node is equal to the height of the right sub-tree

The value of each element in the set is unique, and the system can be automatically sorted based on the value of the element. It should be noted that the value of the elements in the set can not be changed directly. The standard associative container set, multiset, map, Multimap inside C + + STL is a very efficient balanced retrieval binary tree: Red-black trees, also become RB trees (red-black tree). The statistical performance of RB Tree is better than that of general balanced binary tree, so the STL is chosen as the internal structure of the associative container.

Common operations:

Begin () returns the iterator that points to the first element

Clear () Clears all elements

COUNT () returns the number of elements of a value, mostly for existence, with a value of only 0 or 1

Empty () returns True if the collection is null

End () returns an iterator pointing to the last element

Equal_range () returns two iterators in the collection with the upper and lower bounds equal to the given value

Erase () Delete elements in the collection

Find () returns an iterator that points to the element being found

Get_allocator () returns the allocator of the collection

Insert () Inserts an element into the collection

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 collection can hold

Rbegin () returns a reverse iterator that points to the last element in the collection

Rend () returns a reverse iterator that points to the first element in the collection

The number of elements in the size () collection

Swap () swap two set variables

Upper_bound () returns an iterator that is greater than a value element

Value_comp () returns a function to compare the values between elements


1. Element insert: Insert ()

Insert (Key_value); Insert Key_value into set, the return value is Pair<set<int>::iterator,bool>,bool indicates whether the insertion was successful, and iterator represents the insertion position, if Key_ Value is already in set, then iterator represents the position of the key_value in set.

if (Pr.second)
{
cout<<*pr.first<<endl;
}

inset (first,second); Inserts the element between the locator first and second into the set, and the return value is void.
2. Middle sequence traversal: vector-like traversal (with iterators)
3. Reverse traversal: Use the reverse iterator reverse_iterator.
Cases:
Set<int> s;
......
Set<int>::reverse_iterator RIT;
For (Rit=s.rbegin (); Rit!=s.rend (); rit++)
4. Element removal: As with insertions, it can be efficiently removed and automatically adjusted to make the red-black tree balanced.
Set<int> s;
S.erase (2); Delete an element with a key value of 2
S.clear ();

Erase (iterator), remove the value that the locator iterator points to

Erase (First,second), remove the value between the locator first and second

Erase (Key_value), remove the value key_value the key value


5. Element search: Find (), if found, returns the position of the key-value iterator, otherwise, returns a position after the last element.
Set<int> s;
Set<int>::iterator it;
It=s.find (5); Find an element with a key value of 5
if (It!=s.end ())//Found
cout<<*it<<endl;
else//not found
cout<< "not Found";

Lower_bound (Key_value), returns the first locator greater than or equal to Key_value

Upper_bound (Key_value), returns the last locator greater than or equal to Key_value


6. Custom comparison functions
(1) The element is not a struct:
Cases:
Custom comparison function MyComp, overloaded "()" operator
Structmycomp
{
BOOL Operator () (const your_type &a,const your_type&b)
[
Return a.data-b.data>0;
}
}
set<int,mycomp>s;
......
Set<int,mycomp>::iterator it;
(2) If the element is a struct, the comparison function can be written directly in the structure body.
Cases:
Structinfo
{
String name;
Float score;
Overloaded "<" operator, custom collation
BOOL operator < (const Info &a) const
{
Press score to arrange from large to small
Return a.score<score;
}
}
Set<info> s;
......
Set<info>::iterator it;

count () is used to find the number of occurrences of one of the key values in the set. This function is not very useful in set, because a key value can only occur 0 or 1 times in set, which makes it possible to determine if a key value has occurred in set.

Set and multiset automatically sort the elements according to the specific sorting criteria. The difference is that the latter allows elements to be duplicated while the former is not allowed.

Element values cannot be changed directly. Because this will disrupt the original order.

You can change the value of an element by first deleting the old element and inserting the new element.

The access element can only be passed through an iterator, and the element value is constant from the perspective of the iterator.

  1. #include <iostream>
  2. #include <set>
  3. using namespace std;
  4. Program operation will be error. However, if you specify S1 's sorting criteria as greater<int>, it will run successfully.
  5. int Main ()
  6. {
  7. set<int> S1;
  8. set<int,greater<int> > s2;
  9. for (int i = 1;i < 6;++i)
  10. {
  11. S1.insert (i);
  12. S2.insert (i);
  13. }
  14. if (S1 = = s2)
  15. cout << "C1 equals c2!" << Endl;
  16. Else
  17. cout << "C1 not equals C2!" << Endl;
  18. }
  19. Split line here--------------------------------------------------------------------------------------
  20. Several operations of Set
  21. Set_union (A.begin (), A.end (), B.begin (), B.end (), Inserter (C,c.begin ()));
  22. cout << "A u B = {";
  23. print (c);
  24. C.clear ();
  25. Set_intersection (A.begin (), A.end (), B.begin (), B.end (), Inserter (C,c.begin ()));
  26. cout << "A n B = {";
  27. print (c);
  28. C.clear ();
  29. Set_difference (A.begin (), A.end (), B.begin (), B.end (), Inserter (C,c.begin ()));
  30. cout << "A-B = {";
  31. print (c);
  32. C.clear ();
  33. Set_difference (B.begin (), B.end (), A.begin (), A.end (), Inserter (C,c.begin ()));
  34. cout << "SA = {";
  35. print (c);
  36. C.clear ();
  37. Set_difference (A.begin (), A.end (), B.begin (), B.end (), Inserter (C,c.begin ()));
  38. cout << "SB = {";
  39. Print (c)

C + + Set learning Notes

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.