STL Standard Library of C + + learning Notes (vi) Set/multiset associative container

Source: Internet
Author: User

Include header file #include <set> Declaration namespace using namespace Std;

Set/multiset is an associative container that automatically sorts stored elements, and the values of the stored elements cannot be changed directly, only by deleting the old values and inserting new values to change the value of the element. Set does not allow the values of elements to be duplicated, while multiset allows for duplicate elements, which is their only difference. Associative container set, Multiset, and later map, Multimap internal is a very efficient balanced retrieval binary tree: red-black tree.

1) constructor function

Set (); Create an empty set, such as:set<int> A;

Multiset (); Create an empty multiset, such as:multiset<int> A;

Set (const pred& comp); Creates an empty set, specifying the collation according to the comp method

such as: struct COMP//Specify to sort the storage in order from large to small
{
BOOL Operator () (const int &A, const int &b) const
{
Return a>b;
}
};

Multiset (const pred& comp); Creates an empty Multiset, specifying the collation according to the comp method

Set (const set& x); Set copy constructor

Multiset (const multiset& x); Multiset Copy Constructors

Set (InIt first,init last); Copy the elements in a sequence [first,last] sequence into a set

Multiset (InIt first,init last); Copying elements from a sequence [first,last] sequence to Multiset

Set (InIt first,init last,const pred& comp); Copies the elements in a sequence [first,last] sequence into a set, and specifies the collation according to the comp method

such as: int ia[6] = {50,10,60,20,30,100};
Set<int,comp> A (ia,ia+6);
Set<int,comp>::iterator ITER;
For (Iter=a.begin (); Iter!=a.end (); iter++)
{
cout<<*iter<<endl;
}

Program output:

Multiset (InIt first,init last,const pred& comp); Copies the elements in the sequence [first,last] sequence to Multiset, and specifies the collation according to the comp method

2) Traversal function

Iterator begin (); Returns the Set/multiset head pointer, pointing to the first element

Iterator End (); Returns the Set/multiset tail pointer, pointing to the next position of the last element of Set/multiset

Reverse_iterator Rbegin (); Reverse iterator, pointing to the last element

Reverse_iterator rend (); A reverse iterator that points to the position before the first element

3) Judgment function

  BOOL empty () const; When the controlled sequence is empty, the member function returns true

4) Size function

  Size_type size () const; Returns the length of the controlled sequence

Size_type max_size () const; Returns the length of the longest sequence that can be controlled by the container object

5) Interpolation function

  Iterator Insert (const t& val); Multiset, inserting the element Val, and sorting automatically

pair<iterator,bool> Insert (const t& val); Set, insert the element Val, and automatically sort, why the return is pair, there is a bool variable in the pair, the flag insertion is successful, because the set does not allow duplicate elements, so when inserting an element, you need to determine whether the value already exists in the container

Iterator Insert (iterator It,const t& val); Specifies that the element value is inserted in the iterator it at Val. Note that it is only a reference location because set and multiset are automatically sorted, so the insertion position is ultimately determined by the size of the inserted element value, and the insertion position of the input is only the reference position given when the programmer understands the position of the sequence element, which may speed up the insertion speed

void Insert (InIt first,init last); One-time insert (*it) for each element in a sequence [first,last]

6) Delete function

  Iterator Erase (iterator it); Delete the element specified by the iterator it in the controlled sequence

Iterator Erase (iterator first,iterator last); Delete all elements within the range [First,last] in the controlled sequence

Size_type Erase (const key& Key); Deletes an element with an element value equal to key and returns the number of deleted elements

void Clear (); Emptying the controlled sequence

7) Other operation functions

  void swap (set& x); Swap set elements

void swap (multiset& x); Exchanging multiset elements

  Key_compare Key_comp () const; Returns a function for comparing values between elements

Value_compare Value_comp () const; Returns a function to compare the values between elements

Iterator Find (const key& Key) const; Find function that returns an iterator pointer with an element value equal to key

Size_type count (const key& Key) const; Returns the number of elements in the container that are equal to key, because a key value can only be 0 or 1 times in set, so it is used in set to determine whether a key value appears in the set

Iterator Lower_bound (const key& Key) const; Returns an iterator pointer with a value greater than or equal to key in the container

Iterator Upper_bound (const key& Key) const; Returns an iterator pointer with a value greater than key in the container

Pair<iterator,iterator> equal_range (const key& Key) const; Returns a pair of iterators that represent the first element greater than or equal to key and the first element greater than key, and the return value is a pair type

STL Standard Library of C + + learning Notes (vi) Set/multiset associative container

Related Article

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.