Summary of Set/multiset Practice in STL

Source: Internet
Author: User
Tags constant

(1) must include header file before using Set/multiset <set&gt: #include <set>





(2) namespace std{


Template <class T, class Compare = Less<t>,


class allocator = allocator<t> >


class set;


Template <class T, class Compare = Less<t>,


class allocator = allocator<t> >


class Multiset;





can be a set or multiset element type, as long as it is a assignable, copyable, comparable type T.

The ordering criteria for
Set/multiset must be strict weak ordering its meaning: 1 must be objected to, and for the Judgment op, if OP (x,y) is true op (y,x) is false. 2 must be transitive if OP (x,y) is true and OP (y,z) is true op (x,z).  3 must be non reflexive, OP (x,x) is always false.





(3) Set/multiset is usually implemented in a balanced binary tree, in fact, the implementation of the Set/multiset version of most of the red-black tree to achieve, it ensures that node placement can only do two reconnection action,  and the longest path to an element is at most twice times the shortest path depth.





(4) Set/multiset operation function:


Set C produces an empty Set/multiset


Set C (OP) generates an empty Set/multiset using the op-collation criterion


Set C1 (C2) produces a copy of a set/multiset


set C (Beg, end) produces a set/multiset with an element within the interval [beg, end]


set C (Beg, end, op) takes the OP as the sorting criterion and produces a set/multiset with the elements within the interval [beg, end]


C.~set () destroys all elements and frees up memory


where set can be in the following forms:set<elem>, Set<elem, op>, multiset<elem>, Multiset<elem, op>





c.size () returns the current number of elements


C.empty () to determine if C is empty


c.max_size () returns the maximum number of elements that can be accommodated


C1 Compare C2 compare can be ==,!=,<,>,<= and >=





C.count (elem) returns the number of elements with an element value of Elem


C.find (elem) returns an iterator for the first element with an element value of Elem, and returns end if it is not found ()


C.lower_bound (Elem) returns the first placement of Elem, that is, the first element position of "element value >=elem"


C.upper_bound (Elem) returns the last available placement of Elem, which is the first element position of element value >elem


C.equal_range (Elem) returns the first and last position of the Elem, which is the first element interval of the element value ==elem, which returns the return value of Lower_bound () and Upper_bound () to a pair return





C1 = C2 Assign C2 all elements to C1


C1.swap (C2) swaps C1 and C2 elements





C.begin () returns a two-way access iterator (treats the element as a constant), pointing to the first element


C.end () returns a two-way access iterator (which treats the element as a constant), pointing to the next position of the last element


C.rbegin () returns a reverse iterator that points to the first element of the reverse iteration


C.rend () returns a reverse iterator that points to the next position of the last element of the reverse iteration





C.insert (elem) inserts a elem copy and returns the location of the new element


C.insert (it, elem) place the elem copy back to the location of the new element (it is a hint to indicate the search starting point for the placement operation)


C.insert (Beg, end) Inserts a copy of all elements within the interval [beg, end], no return value


c.erase (elem) Removes all elements that are equal to Elem, and returns the number of removed elements


C.erase (IT) removes elements from it location, no return


c.erase (Beg, end) removes all elements from the [Beg, end] interval, no return


C.clear () empties the container


set provides the following interface:


Pair<iterator, bool> Insert (const value_type& elem);

The second member in the
pair structure indicates whether the placement was successful, where the one returned the new element or returned the existing same value element position


iterator Insert (iterator pos_hint, const value_type& elem);


Multiset provides the following interface:


iterator Insert (const value_type& elem);


iterator Insert (iterator pos_hint, const value_type& elem);

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.