C + + STL set User manual

Source: Internet
Author: User

Define a Multiset collection s:
Multiset <int> S;
If you have duplicate elements, be sure to remember to use MULTISET,STL set default from small to large, upper_bound (x) returns the address of the first element greater than x in the collection, Lower_bound (x) returns the address of the first element in the collection that is greater than or equal to X.
We can define an iterator like this: multiset<int>::iterator it;
It points to a memory address that we can make it=s.begin ();
If it is required to follow an element, it can be it++. Requires a precursor to it--。
If the first element less than or equal to X in the collection is required, it can be made: {It=s.upper_bound (X); it--;} (Be careful not to make it out of bounds)
S.insert (x)//Insert an element x
S.clear ()//emptying set S
S.size ()//Returns the number of elements in the collection
S.empty ()//returns a BOOL value indicating whether the collection is empty

Note: the erase () function is very pit.
S.erase (x) Delete the element on this memory address if X is the memory address of an element
If x is an element, delete all elements in s that are equal to this element (note that all)

From small to large traverse the entire set S
Multiset<int>::iterator it;
For (It=s.begin (); It!=s.end (); it++) {//note end condition is it!=s.end () instead of <
cout<< (*it) <<endl;
}

The last element in the collection s:
It=s.end (); it--; S.end () is not the address of the last element in S, so remember it--
cout<< (*it) <<endl;

C + + STL set User manual

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.