Thursday, 21 January 2016

STL sets example

#include <iostream>
#include <set>
#include <vector>
#include <algorithm>

using namespace std;
int main()
{
        int array[] = {9,8,7,9, 6, 5,1,2,3,4,1,2,5};
        //Initialize
        set<int> s(array, array + sizeof(array)/sizeof(array[0]));

        set<int>::iterator it1 = s.end();
        pair<set<int>::iterator, bool> pr;

        vector<int> v(3, 30);

        //Insert
        pr = s.insert(6);
        s.insert(s.end(), 10);
        s.insert(v.begin(), v.end());

        //Delete
        s.erase(s.begin());
        s.erase(s.begin(), ++s.begin());

        //Search
        set<int>::iterator it2 = s.find(7);
        set<int>::iterator it3 = find(s.begin(), s.end(), 7);
        cout<<"Found: "<<*it2<<endl;
        cout<<"Found: "<<*it3<<endl;

        //Traverse
        for(set<int>::iterator it = s.begin(); it != s.end(); it++)
                cout<<*it<<" ";
        cout<<endl;

        //Clear
        s.clear();
}

No comments:

Post a Comment