sets.cpp

来自「C++ sample code, for the book: C++ blac」· C++ 代码 · 共 44 行

CPP
44
字号
#include <iostream>
#include <set>
#include <string>
#include <iterator>
#include <algorithm>
using namespace std;

const int NUMBER_ELEMENTS = 5;

int main()
{
    string strings1[NUMBER_ELEMENTS] = {"A", "B", "C", "D", "E"};
    string strings2[NUMBER_ELEMENTS] = {"C", "D", "E", "F", "G"};

    set<string> first_set(strings1, strings1 + NUMBER_ELEMENTS);
    set<string> second_set(strings2, strings2 + NUMBER_ELEMENTS);

    ostream_iterator<string, char> ostream_itr(cout, " ");

    cout << "First set:" << endl;
    copy(first_set.begin(), first_set.end(), ostream_itr);
    cout << endl;

    cout << "Second set:" << endl;
    copy(second_set.begin(), second_set.end(), ostream_itr);
    cout << endl;
    
    cout << "Difference of the two sets:" << endl;
    set_difference(first_set.begin(), first_set.end(), second_set.begin(), 
        second_set.end(), ostream_itr);
    cout << endl;

    cout << "Intersection of the two sets:" << endl;
    set_intersection(first_set.begin(), first_set.end(), second_set.begin(), 
        second_set.end(), ostream_itr);
    cout << endl;

    cout << "Union of the two sets:" << endl;
    set_union(first_set.begin(), first_set.end(), second_set.begin(), 
        second_set.end(), ostream_itr);
    cout << endl;

    return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?