The path to C ++-standard template library (set ),

Source: Internet
Author: User

The path to C ++-standard template library (set ),

Set: The http://baike.baidu.com/link? Url = cb68AB-3qfEK8RoaGHJFClb4ZiWpJfc32lPOLtaNUrdxntFC738zCZsCiUlfAVRLds2aeNkgYCYCe_wdVB2TIa

The header file <set> must be included;

Definition:

Set <data type> variable name;

Eg:

Set <int> s; // defines an integer set s;

Basic operations (marked in red as common operations ):

S. begin () returns the iterator pointing to the first element.

S. clear () clear all elements

S. count () returns the number of elements of a value.

S. empty () returns true if the set is null.

S. end () returns the next iterator pointing to the last element.

S. equal_range () returns the upper and lower iterator of the set equal to the given value.

S. erase () deletes the elements in the set.

S. find () returns an iterator pointing to the element to be found. // It is said to be a common algorithm. This function is not mentioned in the following code explanation.

S. get_allocator () returns the distributor of the set.

S. insert () insert elements in the Set

S. lower_bound () returns the iterator pointing to the first element greater than (or equal to) a value

S. key_comp () returns a function for comparing values between elements.

S. max_size () returns the maximum value of elements that can be accommodated in the set.

S. rbegin () returns the reverse iterator pointing to the last element in the set.

S. rend () returns the reverse iterator pointing to the first element in the set.

Number of elements in the s. size () Set

Swap () exchange two set variables

S. upper_bound () returns an iterator greater than a certain value element.

S. value_comp () returns a function used to compare values between elements.

Code explanation (to serve the majority of beginners, code interpretation will be followed by a Chinese prompt for operations, do not understand how to stick the code yourself to run ):

# Include <set>
# Include <cstdio>
# Include <string>
# Include <iostream>

Using namespace std;

Int main ()
{
Set <string> strset; // defines a string-type set strset;
Strset. insert ("a"); // Add element;
Strset. insert ("B"); // Add Element B;
Strset. insert ("c"); // Add element c;
Strset. insert ("d"); // Add Element d;
Strset. insert ("a"); // Add element a again;
Cout <"Basic Set Operations:" <endl;
Cout <"whether the set is empty :";
If (strset. empty () printf ("YES \ n ");
Else printf ("NO \ n ");
Set <string >:: iterator = strset. begin ();
Cout <"set traversal:" <endl;
For (; ator! = Strset. end (); ator ++)
Cout <* ator <endl;
Cout <"Number of a in the Set :";
Cout <strset. count ("a") <endl;
Cout <"Number of z in the Set :";
Cout <strset. count ("z") <endl;
String;
Cout <"set heavy operation:" <endl;
Cin>;
If (strset. count (a) = 1) printf ("chong \ n"); // judge whether the element is heavy.
Else printf ("buchong \ n ");
Cout <"set deduplication:" <endl;
Cin>;
If (strset. count (a) = 1) strset. erase (a); // remove duplicate elements.
Ator = strset. begin ();
Cout <"set traversal:" <endl;
For (; ator! = Strset. end (); ator ++)
Cout <* ator <endl;
Cout <"number of elements in the Set :";
Cout <strset. size () <endl;
Cout <"set to 0 operations:" <endl;
Strset. clear (); // clear 0;
Cout <"whether the set is empty :";
If (strset. empty ()/* empty */) printf ("YES \ n ");
Else printf ("NO \ n ");
Return 0;
}

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.