set-constructors

////////////////////////////////////////
//      2018/04/27 19:43:54
//      set-constructors
#include <iostream>
#include <set>
#include <functional>

using namespace std;

int main(){
    int ary[] = { 5, 3, 7, 5, 2, 3, 7, 5, 5, 4 };
    set<int> s1;
    set<int, greater<int>> s2;

    for (int i = 0; i < sizeof(ary) / sizeof(int); i++){
        s1.insert(ary[i]);
        s2.insert(ary[i]);
    }

    set<int>::iterator it = s1.begin();
    cout << "s1:";
    while (it != s1.end()){
        cout << *(it++) << " ";
    }
    cout << endl;

    it = s2.begin();
    cout << "s2:";
    while (it != s2.end()){
        cout << *(it++) << " ";
    }
    cout << endl;

    // second from of constructor
    cout << "s3:";
    set<int> s3(ary, ary + 3);
    it = s3.begin();
    while (it != s3.end()){
        cout << *(it++) << " ";
    }
    cout << endl;

    // copy constructor (predicate of s1 important)
    set<int, less<int>> s4(s1);
    it = s4.begin();
    cout << "s4:";
    while (it != s4.end()){
        cout << *(it++) << " ";
    }
    cout << endl;
    return 0;
}

/*
OUTPUT:
    s1:2 3 4 5 7
    s2:7 5 4 3 2
    s3:3 5 7
    s4:2 3 4 5 7
*/ 
posted @ 2018-04-27 19:59  老耗子  阅读(57)  评论(0编辑  收藏  举报