Some uses of C + + set

Source: Internet
Author: User
Tags set set

Set is also a more common container in STL. The Set collection container realizes the data structure of the red and black tree's balanced binary tree, which automatically adjusts the arrangement of the binary tree and puts the elements in the appropriate position. The values of the elements contained in the set container are unique, and the elements in the collection are arranged in a certain order.

The purpose of constructing set set is to retrieve it quickly and not to modify the key value directly.

Some common operations of set:

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
Erase () Delete elements in the collection
Find () returns an iterator that points to the element being found
Insert () Inserts an element into the collection
Max_size () returns the maximum limit of the elements that the collection can hold
The number of elements in the size () collection
Swap () swap two set variables

In fact, most operations of set are similar to vectors, but set does not support random access and must be accessed using iterators. Because the set is placed in an element, it adjusts the position of the element and puts it in the appropriate position, so there is only one insert insertion operation in the set.

For the collection, we generally have the combination of the set, intersection, difference set, complement these operations, so in the operation of set we also have a similar set of operations, they are in the #include<algorithm> header file:

Std::set_intersection (): This function is to find the intersection of two sets. Std::set_union (): Two set of Std::set_difference (): Difference set std::set_symmetric_difference (): The result is the difference set of the first iterator relative to the second And on the second difference set relative to the first one
School OJ There is a problem can be carried out these several operations, the following is the school OJ problem:

The operation of the description collection is to specify a new collection with the given set. Set A and B are sets, their corresponding complementary sets are defined as follows: A∪b={x|x∈a∨x∈b}a∩b={x|x∈a∧x∈b}a-b={x|x∈a∧x does not belong to B}sa ={x|x∈ (a∪b) ∧x does not belong to A}SB ={x|x∈ (a∪b) ∧x does not belong to B }input The first line, enter a positive integer t, indicating that there is a total of T-Group test data. (t<=200) Then there are 2T lines, each with a n+1 number, where the first number is n (0<=n<=100), indicating that there are also n digital inputs after the line. Output for each set of test data, first outputs the test data sequence number, "Case #.no", then output a total of 7 lines, each line is a set, the first 2 rows of the output set A, B, the next 5 rows to output the set A, B and (a U B), the intersection (a n b), the difference (a–b), the The elements in the collection are expanded with "{}", and the elements are separated by ",". Sample Input1 2 3Sample Outputcase# 1:A = {1, 2, 3}B = {}A U B = {1, 2, 3}A N B = {}A-B = {1, 2, 3}SA = {}SB = {1, 2, 3}
My code is as follows:

#include <iostream> #include <set> #include <algorithm> #include <vector>using namespace std;    void print (Set<int> a) {if (a.begin () = = A.end ()) cout << "}" << Endl;            for (Set<int>::iterator it = A.begin (); It!=a.end (); it++) {if (++it==a.end ()) {it--;        cout << *it << "}\n";            } else {it--;        cout << *it << ",";    }}}int Main () {int T, cou = 0;    Set<int> A, B, C;    Cin >> T;        while (t--) {cou++;        A.clear (), B.clear (), c.clear ();        int n;        CIN >> N;            for (int i=0;i<n;i++) {int x;            CIN >> X;        A.insert (x);        } cin >> N;            for (int i=0;i<n;i++) {int x;            CIN >> X;        B.insert (x); } cout << "case#" << cou << ":" << Endl;       cout << "A = {";        Print (a);        cout << "B = {";        Print (b);        Set_union (A.begin (), A.end (), B.begin (), B.end (), Inserter (C,c.begin ()));        cout << "A u B = {";        print (c);        C.clear ();        Set_intersection (A.begin (), A.end (), B.begin (), B.end (), Inserter (C,c.begin ()));        cout << "A n B = {";        print (c);        C.clear ();        Set_difference (A.begin (), A.end (), B.begin (), B.end (), Inserter (C,c.begin ()));        cout << "A-B = {";        print (c);        C.clear ();        Set_difference (B.begin (), B.end (), A.begin (), A.end (), Inserter (C,c.begin ()));        cout << "SA = {";        print (c);        C.clear ();        Set_difference (A.begin (), A.end (), B.begin (), B.end (), Inserter (C,c.begin ()));        cout << "SB = {";    print (c); } return 0;}
The Inserter is an insert iterator in an iterator adapter.

Principle: Its internal call to insert ()
Function: Inserts an element at the specified position in the container
Limitations: Inserter can be useful only in containers that provide inset () member functions. All STL containers provide the inset () function.
Applicable: All STL containers

Some uses of C + + set

Related Article

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.