[STL Study Notes] Set, Multiset

Source: Internet
Author: User

Set and Multiset will automatically sort the elements according to specific sorting rules. The difference between the two lies in that Multiset allows repeated elements while set does not.

Before using set or Multiset, you must add the header file <set>

Set and Multiset are usually completed by the red and black trees. The red and black trees are excellent in changing the number of elements and searching for elements. It ensures that only two reconnection actions can be performed during node security, in addition, the maximum path depth to an element is only twice the minimum path depth.

Automatic Sorting leads to an important restriction on set and Multiset: you cannot directly change the element value, which will disrupt the original correct order. Therefore, to change the element value, you must first Delete the old element and then insert the new element.

There are two ways to define the sorting criterion:

1. Define with template parameter

2. defined by constructors

Search operation functions of set and Multiset:

Count (ELEM) returns the number of elements whose element value is ELEM.

Find (ELEM) returns the first element whose element value is ELEM. If it cannot be found, end () is returned ()

Lower_bound (ELEM) returns the first pluggable location of ELEM, that is, the first element location where the element value> = ELEM

Upper_bound (ELEM) returns the last pluggable location of ELEM, that is, the first element location of the element value> ELEM.

When _range (ELEM) returns the first and last positions that can be inserted by ELEM, that is, the element range where the element value = ELEM

int main(){    set<int>c;    c.insert(1);    c.insert(2);    c.insert(3);    c.insert(4);    c.insert(5);    c.insert(6);    cout << *c.lower_bound(3) << endl;    cout << *c.upper_bound(3) << endl;    cout << *c.equal_range(3).first << " " << *c.equal_range(3).second << endl;}

Output: 3 4 3 4

 

[STL Study Notes] Set, 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.