Operation of the STL set set, reproduced

Source: Internet
Author: User

STL Set
B add a meaning?The STL can look up this sequence, insert any element of the deleted sequence, and the time to complete these operations is proportional to the logarithm of the number of elements in the sequence, and when the cursor points to a deleted element, the delete operation is invalid. A corrected and more practical definition should be: A collection (set) is a container in which the values of the elements contained are unique. This is useful when collecting the specific value of a data. The elements in the collection are arranged in a certain order, and are used as instances in the collection. A collection is organized by a linked list, which is faster than vectors (vector) on insert and delete operations, but is somewhat slower to find or add elements at the end. The data structure of the balanced binary tree with red and black tree is realized.
1 set and multi-set (set and Multiset container Class)
2 Construction
3 method:
4 set operation:
5 Example:
set and multi-set (set and Multiset container classes)
#include <set>
A collection (set) is a container in which the values of the elements contained are unique.
The difference between set and multi-set is that the set supports unique key values, that the values in the set are specific and occur only once, and that the replica key can appear in Multiset, and the same value can occur more than once.Structure
Explicit Set (const compare&=compare ());
such as:set<int,less<int> > Set1;
Less<int> is a standard class used to form an ascending array of function objects. Descending arrangement is with greater<int>.
Template<class inputiterator> Set (inputiterator, inputiterator,\ const compare&=compare ());
such as: Set<int,less<int> >set2 (Vector1.begin (), Vector1.end ());
Initializes the constructor of a set object by specifying a predefined interval.
Set (const set<key,compare&>);
such as: Set<int,less<int> >set3 (Set2);
The copy constructor.Method:
Begin () returns the iterator that points to the first element
Clear () Clears all elements
COUNT () returns the number of elements of a value
Empty () returns True (True) if the collection is empty
End () returns an iterator that points after the last element, not the last element
Equal_range () returns two iterators in the collection with the upper and lower bounds equal to the given value
Erase () Delete elements in the collection
Find () returns an iterator that points to the element being found
Get_allocator () returns the allocator of the collection
Insert () Inserts an element into the collection
Lower_bound () returns an iterator that points to the first element greater than (or equal to) a value
Key_comp () returns a function for comparing values between elements
Max_size () returns the maximum limit of the elements that the collection can hold
Rbegin () returns a reverse iterator that points to the last element in the collection
Rend () returns a reverse iterator that points to the first element in the collection
The number of elements in the size () collection
Swap () swap two set variables
Upper_bound () returns an iterator that is greater than a value element
Value_comp () returns a function to compare the values between elementsCollection operations:
Std::set_intersection (): This function is to find the intersection of two sets.
Std::set_union (): Ask for a set of two sets
Std::set_difference (): Difference set
Std::set_symmetric_difference (): The result is a difference set of the first iterator relative to the second and a second equivalent to the first
struct compare{
BOOL Operator () (string s1,string s2) {
Return s1>s2;
}///Customizing a copy function
};
std::set<string,compare> s
String str[10];
String *end = Set_intersection (S.begin (), S.end (), S2.begin (), S2.end (), Str,compare ());//intersection, the return value points to the end of the last element of Str
End = Std::set_union (S.begin (), S.end (), S2.begin (), S2.end (), Str,compare ());//Set
End = Std::set_difference (S.begin (), S.end (), S2.begin (), S2.end (), Str,compare ()),//s2 difference set relative to S1
End = Std::set_difference (S2.begin (), S2.end (), S.begin (), S.end (), Str,compare ()),//s1 difference set relative to S2
End = Std::set_symmetric_difference (S.begin (), S.end (), S2.begin (), S2.end (), Str,compare ());//The set of the above two difference setsExample:
1///////////////////////////////////////////////////////////////////
#include <iostream>
#include <set>
using namespace Std;
int main (void)
{
Set<int> Set1;
for (int i=0; i<10; ++i)
Set1.insert (i);
For (Set<int>::iterator p=set1.begin ();p!=set1.end (); ++p)
cout<<*p<< "";
if (Set1.insert (3). Second)//insert 3 into Set1
The insert succeeds Set1.insert (3). Second returns 1, otherwise returns 0
In this example, the concentration already has 3 of this element, so the insert will fail
cout<< "Set insert Success";
Else
cout<< "Set insert Failed";
int a[] = {4, 1, 1, 1, 1, 1, 0, 5, 1, 0};
Multiset<int> A;
A.insert (Set1.begin (), Set1.end ());
A.insert (A,A+10);
cout<<endl;
For (Multiset<int>::iterator p=a.begin ();p!=a.end (); ++p)
cout<<*p<< "";
Cin.get ();
return 0;
}
2////////////////////////////////////////
#include <iostream>
#include <set>
#include <string>
#include <algorithm>
using namespace Std;
struct Compare
{
BOOL Operator () (String s1,string s2)
{
Return s1>s2;
}///Customizing a copy function
};
int main ()
{
typedef std::set<string,compare> _SET;
_set S;
S.insert (String ("SFDSFD"));
S.insert (String ("Apple"));
S.insert (String ("中文版"));
S.insert (String ("dstd"));
cout<< "S1:" <<endl;
Std::set<string,compare>::iterator it = S.begin ();
while (It!=s.end ())
cout<<*it++<< "";
cout<<endl<< "S2:" <<endl;
_set S2;
S2.insert (String ("abc"));
S2.insert (String ("Apple"));
S2.insert (String ("中文版"));
it = S2.begin ();
while (It!=s2.end ())
cout<<*it++<< "";
cout<<endl<<endl;
String str[10];
String *end = Set_intersection (S.begin (), S.end (), S2.begin (), S2.end (), Str,compare ());//intersection, the return value points to the end of the last element of Str
cout<< "Result of Set_intersection s1,s2:" <<endl;
string *first = str;
while (First<end)
cout <<*first++<< "";
cout<<endl<<endl<< "Result of Set_union of S1,s2" <<endl;
End = Std::set_union (S.begin (), S.end (), S2.begin (), S2.end (), Str,compare ());//and set first = str;
while (First<end)
cout <<*first++<< "";
cout<<endl<<endl<< "Result of set_difference of S2 relative to S1" <<endl;
first = str;
End = Std::set_difference (S.begin (), S.end (), S2.begin (), S2.end (), Str,compare ()),//s2 difference from S1 while (First<end)
cout <<*first++<< "";
cout<<endl<<endl<< "Result of set_difference of S1 relative to S2" <<endl;
first = str;
End = Std::set_difference (S2.begin (), S2.end (), S.begin (), S.end (), Str,compare ()),//s1 difference set relative to S2
while (First<end)
cout <<*first++<< "";
cout<<endl<<endl;
first = str;
End = Std::set_symmetric_difference (S.begin (), S.end (), S2.begin (), S2.end (), Str,compare ());//The Set of two difference sets while (first <end)
cout <<*first++<< "";
cout<<endl;
}

Operation of the STL set set, reproduced

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.