Wednesday, October 2, 2013

insert method of STL set

STL set is very convenient to store unique elements following specific order. Normally when inserting element into a set, we seldom check what it returns. Actually the usage of insertion function is very flexible. There are three ways you can use it. Take the following snippet as an example:

    set si;
    while(1){
        pair::iterator,bool> tmp1= si.insert(1); // (1)
        if (tmp1.second){
            cout << "inserted a new value " << *tmp1.first << ".\n";
            set::iterator tmp2=si.insert(tmp1.first,2); // (2)
        }else{
            cout << "an equivalent element "<< *tmp1.first<< " exists already!" << endl;
            int a[]={2,3,4};
            si.insert(a,a+_countof(a)); // (3)
            break;
        }
    }
    copy(si.begin(),si.end(),ostream_iterator(cout," "));cout<


Refer to:
http://www.cplusplus.com/reference/set/set/insert/

No comments: