C + + STL set detailed

Source: Internet
Author: User

I. Explanation

about set must be explained by set associative container. set As a container is also used to store data types of the same data type, and can extract data from a collection of data, in the set unique and the system can be automatically sorted according to the value of the element.

Two. Usage

1. Header files

#include <set

2. Common operation

set<int> s    defines a set container type int type s.begin ()         returns an iterator to the first element s.clear ()          Clears all elements S.count ()         returns the number of elements of a value s.empty ()        If the collection is empty, return Trues.end ()           returns the iterator that points after the last element, not the last element s.erase ()          Removes an element from the collection S.find ()           returns an iterator to the element that was found, and returns end () S.insert () when it is not located () inserts an element in the          collection s.size () the number of elements in the collection            S.swap ()          swap two set variables

Three. Example:

#include <iostream>#include<Set>using namespacestd;intMain () {inti; Set<int>Set1;  for(i=0; i<Ten; ++i) Set1.insert (i); Set<int>:: Iterator it;  for(It=set1.begin (); It!=set1.end (); it++) cout<<*it<<"\ t"; cout<<Endl; Set1.erase (5); if(Set1.insert (3). Second)//Insert 3 into Set1, insert success Set1.insert (3). Second returns 1, otherwise returns 0.cout<<"Set Insert Success"; Elsecout<<"Set Insert Failed"; cout<<Endl; Set<int>:: Iterator ITR;  for(Itr=set1.begin (); Itr!=set1.end (); itr++) cout<<*itr<<"\ t";    Set1.clear (); return 0;}

C + + STL set detailed

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.