Set container of C++stl

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 ascending order, supporting the intersection of the set (set_intersection) Poor (set_difference) and (set_union) symmetry Difference (set_symmetric_difference) such as some operations on a collection, you can use Multiset if you want the elements in the collection to allow repetition .

#include <Set>#include<iterator>#include<iostream>using namespacestd;intMain () {Set<int>v1;//Insert InsertionV1.insert (1); V1.insert (6); V1.insert (5); V1.insert (1);//element 1 is not inserted again in set because it already exists 1V1.insert (Ten); V1.insert (9);
//traversing set, you can see that the elements are orderedSet<int>:: iterator it; for(It=v1.begin (); It!=v1.end (); it++) cout<<*it<<" "; cout<<Endl;//Use the size () function to get the current number of elementsCout<<v1.size () <<Endl;if(V1.find ( $) ==v1.end ())//The Find () function can find out whether an element existscout<<"v1 ' t in the set"<<endl;//find will look up the set,
When it arrives at Set.end (), it is not found, return end ()Set<int>v2; for(intI=6;i< the; i++) V2.insert (i); for(It=v2.begin (); It!=v2.end (); it++) cout<<*it<<" "; cout<<Endl;Set<int>v3;//get two set andSet_union (V1.begin (), V1.end (), V2.begin (), V2.end (),insert_iterator<Set<int> > (V3,v3.begin ()));//Note the form of a fifth parameterCopy (V3.begin (), V3.end (),ostream_iterator<int> (cout," ")); cout<<endl;v3.clear ();//Note set to receive results before the collection operation to call the clear () function to clear the//get two set of the intersectionSet_intersection (V1.begin (), V1.end (), V2.begin (), V2.end (),insert_iterator<Set<int> >(V3,v3.begin ())); Copy (V3.begin (), V3.end (), Ostream_iterator<int> (cout," ")); cout<<Endl;
//gets the difference of two setv3.clear (); Set_difference (V1.begin (), V1.end (), V2.begin (), V2.end (), Insert_iterator<Set<int> >(V3,v3.begin ()));copy (V3.begin (), V3.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∩bv3.clear (); Set_symmetric_difference (V1.begin (), V1.end (), V2.begin (), V2.end (), Insert_iterator<Set<int> >(V3,v3.begin ())); Copy (V3.begin (), V3.end (), Ostream_iterator<int> (cout," ")); cout<<Endl;return 0;}

Set container of C++stl

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.