Set (set): Http://baike.baidu.com/link?url= Cb68ab-3qfek8roaghjfclb4ziwpjfc32lpoltanurdxntfc738zczsciulfavrlds2aenkgycyce_wdvb2tia
Need to include header files <set>;
Defined:
set< data type > variable name;
Eg:
Set<int> s;//defines a set of integral s;
Basic operation (Red is identified as a common operation):
s.begin () returns the iterator that points to the first element
S.clear () clears all elements
S. Count () returns the number of elements of a value
s.empty () returns True if the collection is empty
s.end () returns an iterator pointing to the last element
S.equal_range () returns two iterators in the collection with the upper and lower bounds equal to the given value
s.erase () delete elements in the collection
s.find () returns an iterator to the element being found//not very useful, it is said to be a common algorithm, the function is not mentioned in the following code explanation.
S.get_allocator () returns the allocator of the collection
S.insert () inserting elements in the collection
S.lower_bound () returns an iterator that points 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 limit of the elements that the collection can hold
S.rbegin () Returns a reverse iterator that points to the last element in the collection
S.rend () Returns a reverse iterator that points to the first element in the collection
s.size () the number of elements in the collection
Swap () swap two set variables
S.upper_bound () returns an iterator that is greater than a value element
S.value_comp () returns a function to compare the values between elements
Code interpretation (for the service of the vast number of beginners, after the code to explain the operation of Chinese hints, do not understand the sticky code to run themselves):
#include <set>
#include <cstdio>
#include <string>
#include <iostream>
using namespace Std;
int main ()
{
Set<string> strset;//defines a collection strset that is a string type;
Strset.insert ("a");//add element A;
Strset.insert ("B");//add element B;
Strset.insert ("C");//add element C;
Strset.insert ("D");//add element D;
Strset.insert ("a");//add element A;
cout<< "Set basic operations:" <<endl;
cout<< "Whether the collection is empty:";
if (Strset.empty ()) printf ("yes\n");
else printf ("no\n");
Set<string>::iterator Ator=strset.begin ();
cout<< "collection traversal:" <<endl;
for (; Ator!=strset.end (); ator++)
cout<<*ator<<endl;
cout<< "The number of a in the set:";
Cout<<strset.count ("a") <<endl;
cout<< "The number of Z in the collection:";
Cout<<strset.count ("z") <<endl;
String A;
cout<< "collection and re-operation:" <<endl;
cin>>a;
if (Strset.count (a) ==1) printf ("chong\n");//Determines whether the element is heavy or not.
else printf ("buchong\n");
cout<< "set to redo operation:" <<endl;
cin>>a;
if (Strset.count (a) ==1) strset.erase (a);//Remove heavy elements.
Ator=strset.begin ();
cout<< "collection traversal:" <<endl;
for (; Ator!=strset.end (); ator++)
cout<<*ator<<endl;
cout<< "The number of elements in the collection:";
Cout<<strset.size () <<endl;
cout<< "Set Clear 0 operation:" <<endl;
Strset.clear ();//clear 0;
cout<< "Whether the collection is empty:";
if (Strset.empty ()/*/empty */) printf ("yes\n");
else printf ("no\n");
return 0;
}
The path of C + +--Standard Template Library (set)