1. 程式人生 > >C++學習 std::set作為一個有序集合

C++學習 std::set作為一個有序集合

#include<set>
#include<iterator>
#include<iostream>
#include <algorithm>
using namespace std;
int main()
{
    set<int> eg1;
    eg1.insert(1);
    eg1.insert(100);
    eg1.insert(5);
    eg1.insert(1); //元素1因為已經存在所以set中不會再次插入1
    eg1.insert(10);
    eg1.insert(9);
    //遍歷set,可以發現元素是有序的
    set<int>::iterator set_iter=eg1.begin();
    cout<<"Set named eg1:"<<endl;
    for(;set_iter!=eg1.end();set_iter++) cout<<*set_iter<<" ";
    cout<<endl;
    //使用size()函式可以獲得當前元素個數
    cout<<"Now there are "<<eg1.size()<<" elements in the set eg1"<<endl;
    if(eg1.find(200)==eg1.end())//find()函式可以查詢元素是否存在
        cout<<"200 isn't in the set eg1"<<endl;


    set<int> eg2;
    for(int i=6;i<15;i++)
        eg2.insert(i);
    cout<<"Set named eg2:"<<endl;
    for(set_iter=eg2.begin(); set_iter!=eg2.end(); set_iter++)
        cout<<*set_iter<<" ";
    cout<<endl;

    //獲得兩個set的並
    set<int> eg3;
    cout<<"Union(兩個set的並集):";
    set_union(eg1.begin(),
              eg1.end(),
              eg2.begin(),
              eg2.end(),
              insert_iterator<set<int> >(eg3, eg3.begin())
              );//注意第五個引數的形式
    copy(eg3.begin(), eg3.end(), ostream_iterator<int>(cout," "));
    cout<<endl;

    //獲得兩個set的交,注意進行集合操作之前接收結果的set要呼叫clear()函式清空一下
    eg3.clear();
    set_intersection(eg1.begin(),
                     eg1.end(),
                     eg2.begin(),
                     eg2.end(),
                     insert_iterator<set<int> >(eg3,eg3.begin())
                     );
    cout<<"Intersection:";
    copy(eg3.begin(),eg3.end(),ostream_iterator<int>(cout," "));
    cout<<endl;

    //獲得兩個set的差
    eg3.clear();
    set_difference(eg1.begin(),
        eg1.end(),eg2.begin(),
        eg2.end(),
        insert_iterator<set<int> >(eg3,eg3.begin())
        );
    cout<<"Difference:";
    copy(eg3.begin(),eg3.end(),ostream_iterator<int>(cout," "));
    cout<<endl;

    //獲得兩個set的對稱差,也就是假設兩個集合分別為A和B那麼對稱差為AUB-A∩B
    eg3.clear();
    set_symmetric_difference(eg1.begin(),eg1.end(),eg2.begin(),eg2.end(),insert_iterator<set<int> >(eg3,eg3.begin()));
    copy(eg3.begin(),eg3.end(),ostream_iterator<int>(cout," "));
    cout<<endl;

    return 0;
}

下面給出一個關鍵字型別為char*的示例程式碼

#include<iostream>
#include<iterator>
#include<set>
using namespace std;
struct ltstr
{
bool operator() (const char* s1, const char* s2) const
{
    return strcmp(s1, s2) < 0;
}
};

int main()
{
const int N = 6;
const char* a[N] = {"isomer", "ephemeral", "prosaic",
    "nugatory", "artichoke", "serif"};
const char* b[N] = {"flat", "this", "artichoke",
    "frigate", "prosaic", "isomer"};

set<const char*,ltstr> A(a, a + N);
set<const char*,ltstr> B(b, b + N);
set<const char*,ltstr> C;

cout << "Set A: ";
//copy(A.begin(), A.end(), ostream_iterator<const char*>(cout, " "));
set<const char*,ltstr>::iterator itr;
for(itr=A.begin();itr!=A.end();itr++) cout<<*itr<<" ";
cout << endl;
cout << "Set B: ";
copy(B.begin(), B.end(), ostream_iterator<const char*>(cout, " "));   
cout << endl;

cout << "Union: ";
set_union(A.begin(), A.end(), B.begin(), B.end(),
     ostream_iterator<const char*>(cout, " "),
     ltstr());   
cout << endl;

cout << "Intersection: ";
set_intersection(A.begin(), A.end(), B.begin(),B.end(),ostream_iterator<const char*>(cout," "),ltstr());
cout<<endl;
set_difference(A.begin(), A.end(), B.begin(), B.end(),inserter(C, C.begin()),ltstr());
cout << "Set C (difference of A and B): ";
copy(C.begin(), C.end(), ostream_iterator<const char*>(cout, " "));
cout <<endl;
return 0;
}

其中的ltstr也可以這樣定義
class ltstr
{
         public:
         bool operator() (const char* s1,const char*s2)const
         {
                 return strcmp(s1,s2)<0;
         }
};

更加通用的應用方式那就是資料型別也是由使用者自定義的類來替代,比較的函式自定義,甚至可以加上二級比較,比如首先按照總分數排序,對於分數相同的按照id排序,下面是示例程式碼

#include<set>
#include<iostream>
using namespace std;
struct
{
                 int id;
                 int score;
                 string name;
};
struct compare
{
         bool operator()(const Entity& e1,const Entity& e2)const    {
                 if(e1.score<e2.score) return true;
                 else
                         if(e1.score==e2.score)
                                 if(e1.id<e2.id) return true;

                 return false;
         }
};

int main()
{
         set<Entity,compare>s_test;
         Entity a,b,c;
         a.id=123;a.score=90;a.name="bill";
         b.id=121;b.score=85;b.name="mary";
         c.id=130;c.score=85;c.name="jerry";
         s_test.insert(a);s_test.insert(b);s_test.insert(c);
         set<Entity,compare>::iterator itr;
         cout<<"Score List(ordered by score):\n";
         for(itr=s_test.begin();itr!=s_test.end();itr++)
                 cout<<itr->id<<"---"<<itr->name<<"---"<<itr->score<<endl;
         return 0;
}