Set set for the standard template library (STL) Learning Guide

Source: Internet
Author: User
Tags set set

Set is the associated container. The key value is the real value, the real value is the key value, and there cannot be duplicates. Therefore, we cannot change the value of the Set element through the set iterator. Set has the same characteristics as list: when inserting or deleting an iterator, The iterator before the operation is still valid. Of course, the deleted one will be ineffective. The underlying structure of set is RB-tree, so it is ordered.

STL provides an algorithm for Set Operations: intersection set_intersection, Union set set_union, and difference set set_difference. The symmetry difference set set_symeetric_difference, which will be discussed later.

1. Set template class declaration.

template <   class key   class =Traitsless<key>    class Allocator=allocator<key>>class set。


The meanings of these parameters are as follows:

Key: the data type to be put into the set. It can be any type of data.

Traits: This is an imitation function (I will talk about it later ). Provides a functional imitation function to find the order of elements in the set. This is an optional parameter. The default value is STD: less <key>, if you want to provide this parameter by yourself, you must follow this rule: with two parameters, the return type is bool.

Allocator: the Space configurator. this parameter is optional. The default value is STD: Allocator <key>.

Ii. Basic operations in Set

We can instantiate a set object using the following method:

STD: Set <int> S; the elements stored in the S object are sorted in ascending order (because STD: less is used as a comparison tool .)

If you want to insert data in S, you can use the inset function (set does not use the reload [] operation, because the set value is the same as the index value)

S. insert (3); S. insert (5 ).....

Because set is a set, the set itself must be unique. Therefore, if you want to insert data in the set to overlap with the previous data, the insertion will fail.

You can use the following method to traverse the elements in the set.

STD: Set <int >:: iterator it = S. Begin (); While (it! = S. End () {cout <* It ++ <Endl; // iterator moves backward until the end .}


If you want to find an element using the find function, it = S. Find (3); in this way, it points to the element 3. You can reverse traverse through rbegin and rend.

std::set<int>::reverse_iterator it = s.rbegin();while(it!=s.rend()){  cout<<*it++<<endl;}


Other operations are not listed here.

Iii. Example of using the set vector

# Include <iostream> # include <string> # include <set> # include <algorithm> # include <iterator> using namespace STD; /* the Union container combines the value with the keyword and uses the keyword to search for the value. * quick access to the element is provided. The inserted element cannot be specified, the container automatically processes the Insert Location * STL provides four types of combined containers: Set, Multiset, MAP, multimap * set, and Multiset storage. The former keywords cannot be repeated, and the latter keywords can be repeated. * Map and multimap store a pair of element keys and values. The former keywords cannot be repeated, and the latter keywords can be repeated. */INT main () {const int n = 3; string S1 [N] = {"XP", "Python", "Linux "}; string S2 [N] = {"Python", "php", "Perl"}; set <string> SA (S1, S1 + n); // declare a set sa, element is an array S1 set <string> Sb (S2, S2 + n); // declares a set sb, element is an array S2 set <string> SC; // declare an empty set SC ostream_iterator <string, char> out (cout, ""); copy (SA. begin (), SA. end (), OUT); cout <"-> set sa" <Endl; copy (sb. begin (), sb. end (), OUT); cout <"-> set sb" <Endl; set_union (SA. begin (), SA. end (), sb. begin (), sb. end (), OUT); cout <"-> set_union () Union" <Endl; set_intersection (SA. begin (), SA. end (), sb. begin (), sb. end (), OUT); cout <"-> set_intersection () intersection" <Endl; set_difference (SA. begin (), SA. end (), sb. begin (), sb. end (), OUT); cout <"-> set_difference () set difference" <Endl; set_difference (sb. begin (), sb. end (), SA. begin (), SA. end (), OUT); cout <"-> set_difference () set difference" <Endl; set_union (SA. begin (), SA. end (), sb. begin (), sb. end (), insert_iterator <set <string> (SC, SC. begin (); SC. insert ("Delphi"); copy (SC. begin (), SC. end (), OUT); cout <"-> set SC" <Endl; copy (SC. lower_bound ("Perl"), SC. upper_bound ("Python"), OUT); cout <"-> Display Set range" <Endl; return 0 ;}

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.