"C + + STL" set and Multiset

Source: Internet
Author: User

1. Structure

Set and Multiset sort the elements according to a particular sort principle. The difference is that multisets allows elements to be duplicated, and set does not allow duplicates.

As long as the type T is assignable, copyable, comparable (according to a sort criterion), it can be the element of set or multisets. If there is no special sorting principle, with the default less, the elements are operator < compared to complete the sorting.

The sorting criteria must adhere to the following principles:

    • Must be "objected to", for operator <, if x < Y is true, Y<x is false.
    • Must be "transitive", for operator <, if X<y is true, Y<z is true, then X<z is true.
    • Must be "non-reflexive", for operator<, X<x is always false.
2. Ability

Like all standard associative containers, sets and multisets are usually done with a balanced binary tree.

The main advantage of automatic sorting is that the two-tree search element has good performance, and its search function algorithm has logarithmic complexity. However, automatic sorting also creates a limit that cannot be changed directly, because it disrupts the order in which the element is changed, and the old element must be removed before the new element is inserted. So sets and multisets have the following characteristics:

    • Does not provide any manipulation elements that are directly used to access the element
    • The element is accessed through an iterator.
3, the Operation function 3.1 constructs, copies, the destruction

Operation

Effect

Set C

Produces an empty set/multiset with no elements

Set C (OP)

Using OP as the ordering criterion, an empty set/multiset is generated.

Set C1 (C2)

Produce a copy of a set/multiset, all elements are copied

Set C (beg,end)

Generates a set/multiset with all elements within the interval [beg,end]

Set C (Beg,end, op)

With OP as the ordering criterion, the elements within the interval [beg,end] produce a set/multiset

C.~set ()

Destroy all elements and free up memory

Set<elem>

Produces a set, with (operator <) as the sorting criteria

Set<elem,0p>

Produces a set, with Op as the sorting criterion

3.2 Non-volatile operation

Operation

Effect

C.size ()

Returns the current number of elements

C.empty ()

Determines whether the size is zero, equal to 0 = = size (), higher efficiency

C.max_size ()

Returns the maximum number of elements that can be accommodated

C1 = = C2

Determine if C1 equals C2

C1! = C2

Determine if C1 is not equal to C2 (equal to!) ( C1==C2))

C1 < C2

Determine if C1 is less than C2

C1 > C2

Determine if C1 is greater than C2

C1 <= C2

Determine if C1 is less than or equal to C2 (equal to!) ( C2<C1))

C1 >= C2

Determine if C1 is greater than or equal to C2 (equal to!) ( C1<C2))

3.3 Special Search functions

Sets and multisets are optimized for element-fast search, providing special search functions, so the search function should be used preferentially to obtain logarithmic complexity rather than the linear complexity of the STL. For example, in 1000 element search, the logarithm complexity average 10 times, and the linear complexity average needs 500 times.

Operation

Effect

Count (Elem)

Returns the number of element values to Elem

Find (Elem)

Returns the element value as the first element of Elem if no end () is returned

Lower _bound (elem)

Returns the first placement of the element value Elem, which is the first element position of the element value >= Elem

Upper _bound (elem)

Returns the last elem position of the element value, which is the first element position of the element value > Elem

Equal_range (Elem)

Returns the first and last position where the elem can be placed, i.e. the interval of the element value ==elem

3.4 Assigning values

Operation

Effect

C1 = C2

Give all the elements of C2 to C1

C1.swap (C2)

Swap elements of C1 and C2

Swap (C1,C2)

Ibid., Global function

3.5 iterator-related functions

Sets and Multisets iterators are bidirectional iterators, and all elements are treated as constants to the iterator operation, ensuring that you do not artificially alter the values of the elements, disrupting the established order, and therefore cannot invoke the variable algorithm, such as remove ().

Operation

Effect

C.begin ()

Returns a random access iterator that points to the first element

C.end ()

Returns a random access iterator that points to the next position of the last element

C.rbegin ()

Returns a reverse iterator that points to the first element of a reverse iteration

C.rend ()

Returns a reverse iterator that points to the next position of the last element of the reverse iteration

3.6 Inserting and deleting elements

The argument must be valid, the iterator must point to a valid position, the sequence start point cannot be located after the end point, and the element cannot be removed from the empty container.

Operation

Effect

C.insert (Elem)

Inserts a elem copy, returning the new element position, regardless of whether the insert succeeds or not.

C.insert (POS, Elem)

Insert a copy of the Elem element, return the new element position, POS as the starting point of the collection, increase the insertion speed.

C.insert (Beg,end)

Place all elements of the interval [beg,end] into C, with no return value.

C.erase (Elem)

Removes all elements equal to Elem and returns the number of elements removed.

C.erase (POS)

Removes the position element referred to by the iterator Pos, with no return value.

C.erase (Beg,end)

Removes all elements of the interval [beg,end], with no return value.

C.clear ()

Remove all elements to empty the container

4. Example code 4.1 set
//Cont/set1.cpp#include<iostream>#include<Set>using namespacestd; intMain () {/*type of the collection: *-no duplicates *-elements is integral values *-descending order */typedefSet<int,greater<int> >Intset;         Intset coll1; //Empty Set Container//insert elements in random orderColl1.insert (4); Coll1.insert (3); Coll1.insert (5); Coll1.insert (1); Coll1.insert (6); Coll1.insert (2); Coll1.insert (5); //iterate over all elements and print themIntset::iterator POS;  for(pos = Coll1.begin (); pos! = Coll1.end (); + +POS) {cout<< *pos <<' '; } cout<<Endl; //Insert 4 again and process return valuePair<intset::iterator,BOOL> Status = Coll1.insert (4); if(status.second) {cout<<"4 inserted as Element"<< distance (Coll1.begin (), status. First) +1<<Endl; }       Else{cout<<"4 already exists"<<Endl; }       //assign elements to another set with ascending order       Set<int>coll2 (Coll1.begin (), Coll1.end ()); //Print all elements of the copycopy (Coll2.begin (), Coll2.end (), Ostream_iterator<int> (cout," ")); cout<<Endl; //Remove all elements-to- element with value 3Coll2.erase (Coll2.begin (), Coll2.find (3)); //remove all elements with value 5       intnum; Num= Coll2.erase (5); cout<< Num <<"element (s) removed"<<Endl; //Print all elementscopy (Coll2.begin (), Coll2.end (), Ostream_iterator<int> (cout," ")); cout<<Endl; }

Output:

   6 5 4 3 2 1   4 already exists    1 2 3 4 5 6   1 element (s) removed    3 4 6
4.2 Multiset
 //Cont/mset1.cpp#include<iostream>#include<Set>using namespacestd; intMain () {/*type of the collection: *-duplicates allowed *-elements is integral values *-descending order */typedef multiset<int,greater<int> >Intset; Intset Coll1,//Empty multiset Container//insert elements in random orderColl1.insert (4); Coll1.insert (3); Coll1.insert (5);       Coll1.insert (l); Coll1.insert (6); Coll1.insert (2); Coll1.insert (5); //iterate over all elements and print themIntset::iterator POS;  for(pos = Coll1.begin (); pos! = Coll1.end (); + +POS) {cout<< *pos <<' '; } cout<<Endl; //Insert 4 again and process return valueIntset::iterator ipos = Coll1.insert (4); cout<<"4 inserted as Element"<< distance (Coll1.begin (), IPOs) +1<<Endl; //assign elements to another multiset with ascending ordermultiset<int>coll2 (Coll1.begin (), Coll1.end ()); //Print all elements of the copycopy (Coll2.begin (), Coll2.end (), Ostream_iterator<int> (cout," ")); cout<<Endl; //Remove all elements-to- element with value 3Coll2.erase (Coll2.begin (), Coll2.find (3)); //remove all elements with value 5       intnum; Num= Coll2.erase (5); cout<< Num <<"element (s) removed"<<Endl; //Print all elementscopy (Coll2.begin (), Coll2.end (), Ostream_iterator<int> (cout," ")); cout<<Endl; }

Output:

6 5 5 4 3 2 1 4  as 5 1 2 3 4 4 5 5 6 2 element (s) removed 3 4 4 6

C + + STL set and multiset

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.