Go C + + STL set () collection

Source: Internet
Author: User

Set is a standard associative container in STL (Vector,list,string,deque is a sequence container, and Set,multiset,map,multimap is a standard associative container), which is implemented using a balanced search tree-red and black trees. Insert delete operation only need pointer operation node to complete, do not involve memory move and copy, so efficiency is high. Set, as the name implies, is the meaning of "set", the elements are unique in set, and by default the elements are automatically sorted in ascending order, supporting the intersection (set_intersection) of the set, the difference (set_difference) and (set_union), the symmetric difference ( Set_symmetric_difference) and so on some set of operations, if you need the elements in the collection to allow repetition then you can use Multiset
#include <set>
#include <iterator>
#include <iostream>
using namespace Std;
int main ()
{
set<int>eg1;
Insert
Eg1.insert (1);
Eg1.insert (100);
Eg1.insert (5);
Eg1.insert (1);//element 1 is not inserted again in set because it already exists 1
Eg1.insert (10);
Eg1.insert (9);
Traversing set, you can see that the elements are ordered
Set<int>::iterator Set_iter=eg1.begin ();
cout<< "Set named EG1:" <<endl;
for (; Set_iter!=eg1.end (); set_iter++) cout<<*set_iter<< "";
cout<<endl;
Use the size () function to get the current number of elements
cout<< "Now there is" <<eg1.size () << "elements in the set EG1" <<endl;
The IF (Eg1.find () ==eg1.end ())//find () function can find out if an element exists
cout<< "T in the set EG1" <<endl;

set<int>eg2;
for (int i=6;i<15;i++)
Eg2.insert (i);
cout<< "Set named EG2:" <<endl;
For (Set_iter=eg2.begin (); Set_iter!=eg2.end (); set_iter++)
cout<<*set_iter<< "";
cout<<endl;
Get two set and
set<int>eg3;
cout<< "Union:";
Set_union (Eg1.begin (), Eg1.end (), Eg2.begin (), Eg2.end (),insert_iterator<set<int> > (Eg3,eg3.begin ())) ;//Note the form of a fifth parameter
Copy (Eg3.begin (), Eg3.end (),ostream_iterator<int> (cout, ""));
cout<<endl;
Get two sets of the intersection, note the set to receive the result before collection operation to call the clear () function to clear the
Eg3.clear ();
Set_intersection (Eg1.begin (), Eg1.end (), Eg2.begin (), Eg2.end (),insert_iterator<set<int> > (EG3, Eg3.begin ()));
cout<< "intersection:";
Copy (Eg3.begin (), Eg3.end (),ostream_iterator<int> (cout, ""));
cout<<endl;
Gets the difference of two set
Eg3.clear ();
Set_difference (Eg1.begin (), Eg1.end (), Eg2.begin (), Eg2.end (),insert_iterator<set<int> > (eg3,eg3.begin ()));
cout<< "Difference:";
Copy (Eg3.begin (), Eg3.end (),ostream_iterator<int> (cout, ""));
cout<<endl;
The symmetry difference of two sets is obtained, that is, if the two set is a and B, then the symmetry difference is aub-a∩b
Eg3.clear ();
Set_symmetric_difference (Eg1.begin (), Eg1.end (), Eg2.begin (), Eg2.end (),insert_iterator<set<int> > (eg3 , Eg3.begin ()));
Copy (Eg3.begin (), Eg3.end (),ostream_iterator<int> (cout, ""));
cout<<endl;

return 0;
}

Set will sort the elements, so the question is what sort of rules are there? The above example code we found that the elements of the int type can automatically determine the size order, but the char* will not be automatically judged by strcmp, let alone the user-defined type, in fact set the standard form is Set<key, Compare, alloc> ,

Parameters Description Default Value
Key types of keywords and values for a collection
Compare The keyword comparison function, which is the parameter type of the key parameter, returns true if the first argument is less than the second argument, otherwise false Less<key>
Alloc Set allocator for internal memory management Alloc

Here is a sample code with a keyword type of char*

#include <iostream>
#include <iterator>
#include <set>
using namespace Std;
struct LTSTR
{
BOOL Operator () (const char* S1, const char* s2) const
{
Return strcmp (S1, S2) < 0;
}
};

int main ()
{
const int N = 6;
Const char* A[n] = {"Isomer", "ephemeral", "prosaic",
"Nugatory", "artichoke", "serif"};
Const char* B[n] = {"Flat", "this", "artichoke",
"Frigate", "prosaic", "isomer"};

Set<const char*,ltstr> A (a, A + N);
Set<const char*,ltstr> B (b, B + N);
Set<const char*,ltstr> C;

cout << "Set A:";
Copy (A.begin (), A.end (), Ostream_iterator<const char*> (cout, ""));
Set<const Char*,ltstr>::iterator ITR;
For (Itr=a.begin (); Itr!=a.end (); itr++) cout<<*itr<< "";
cout << Endl;
cout << "Set B:";
Copy (B.begin (), B.end (), Ostream_iterator<const char*> (cout, ""));
cout << Endl;

cout << "Union:";
Set_union (A.begin (), A.end (), B.begin (), B.end (),
Ostream_iterator<const char*> (cout, ""),
Ltstr ());
cout << Endl;

cout << "intersection:";
Set_intersection (A.begin (), A.end (), B.begin (), B.end (), Ostream_iterator<const char*> (cout, ""), Ltstr ());
cout<<endl;
Set_difference (A.begin (), A.end (), B.begin (), B.end (), Inserter (C, C.begin ()), Ltstr ());
cout << "Set C (difference of A and B):";
Copy (C.begin (), C.end (), Ostream_iterator<const char*> (cout, ""));
cout <<endl;
return 0;
}

Ltstr can also be defined as such
Class Ltstr
{
Public
BOOL Operator () (const char* s1,const char*s2) const
{
Return strcmp (S1,S2) <0;
}
};

More general application way that is, the data type is also a user-defined class to replace, the comparison of function customization, and even can be added two-level comparison, such as first sorted by total number of points, for the same score by ID, the following is the sample code

#include <set>
#include <iostream>
using namespace Std;
struct
{
int id;
int score;
String name;
};
struct Compare
{
BOOL Operator () (const entity& E1,CONST entity& E2) Const {
if (E1.score<e2.score) return true;
Else
if (E1.score==e2.score)
if (e1.id<e2.id) return true;

return false;
}
};

int main ()
{
set<entity,compare>s_test;
Entity A,b,c;
A.id=123;a.score=90;a.name= "Bill";
B.id=121;b.score=85;b.name= "Mary";
C.id=130;c.score=85;c.name= "Jerry";
S_test.insert (a); S_test.insert (b); S_test.insert (c);
Set<entity,compare>::iterator ITR;
cout<< "Score List (ordered by score): \ n";
For (Itr=s_test.begin (); Itr!=s_test.end (); itr++)
cout<<itr->id<< "---" <<itr->name<< "---" <<itr->score<<endl;
return 0;
}

Go C + + STL set () collection

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.