程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> C++中使用stl的set和hash_set

C++中使用stl的set和hash_set

編輯:C++入門知識

set的使用:     [cpp]  #include<iostream>    #include<set>    #include<string>    #include<iterator>    #include<algorithm>//set_union()       using namespace std;         void main(){       string s1[3]={"aaa","bbb","ccc"};       string s2[3]={"bbb","ddd","eee"};          set<string> sa(s1,s1+3);//申明一個set集合,元素為數組s1        set<string> sb(s2,s2+3);//申明一個set集合,元素為數組s2        set<string>   sc;//申明一個空set集合               cout<<"輸出sa的元素:";       copy(sa.begin(),sa.end(),ostream_iterator<string>(cout," "));              cout<<"\n\n輸出sb的元素:";       copy(sb.begin(),sb.end(),ostream_iterator<string>(cout," "));              cout<<"\n\n輸出sa和sb的並集:";       set_union(sa.begin(),sa.end(),sb.begin(),sb.end(),ostream_iterator<string>(cout," "));              cout<<"\n\n輸出sa和sb的交集:";       set_intersection(sa.begin(),sa.end(),sb.begin(),sb.end(),ostream_iterator<string>(cout," "));              cout<<"\n\n輸出sa-sb:";       set_difference(sa.begin(),sa.end(),sb.begin(),sb.end(),ostream_iterator<string>(cout," "));                     cout<<"\n\n輸出sb-sa:";       set_difference(sb.begin(),sb.end(),sa.begin(),sa.end(),ostream_iterator<string>(cout," "));                     set_union(sa.begin(),sa.end(),sb.begin(),sb.end(),insert_iterator<set<string>>(sc,sc.begin()));       sc.insert("fff");       copy(sc.begin(),sc.end(),ostream_iterator<string>(cout," "));       cout<<endl;       cout<<"\n輸出處理後的sc:";       copy(sc.lower_bound("ddd"),sc.upper_bound("fff"),ostream_iterator<string>(cout," "));       cout<<endl<<endl;      }     #include<iostream> #include<set> #include<string> #include<iterator> #include<algorithm>//set_union()   using namespace std;     void main(){ string s1[3]={"aaa","bbb","ccc"}; string s2[3]={"bbb","ddd","eee"};   set<string> sa(s1,s1+3);//申明一個set集合,元素為數組s1 set<string> sb(s2,s2+3);//申明一個set集合,元素為數組s2 set<string> sc;//申明一個空set集合   cout<<"輸出sa的元素:"; copy(sa.begin(),sa.end(),ostream_iterator<string>(cout," "));   cout<<"\n\n輸出sb的元素:"; copy(sb.begin(),sb.end(),ostream_iterator<string>(cout," "));   cout<<"\n\n輸出sa和sb的並集:"; set_union(sa.begin(),sa.end(),sb.begin(),sb.end(),ostream_iterator<string>(cout," "));   cout<<"\n\n輸出sa和sb的交集:"; set_intersection(sa.begin(),sa.end(),sb.begin(),sb.end(),ostream_iterator<string>(cout," "));   cout<<"\n\n輸出sa-sb:"; set_difference(sa.begin(),sa.end(),sb.begin(),sb.end(),ostream_iterator<string>(cout," "));     cout<<"\n\n輸出sb-sa:"; set_difference(sb.begin(),sb.end(),sa.begin(),sa.end(),ostream_iterator<string>(cout," "));     set_union(sa.begin(),sa.end(),sb.begin(),sb.end(),insert_iterator<set<string>>(sc,sc.begin())); sc.insert("fff"); copy(sc.begin(),sc.end(),ostream_iterator<string>(cout," ")); cout<<endl; cout<<"\n輸出處理後的sc:"; copy(sc.lower_bound("ddd"),sc.upper_bound("fff"),ostream_iterator<string>(cout," ")); cout<<endl<<endl;   }                    hash_set (在Dev C++ 和Linux記得加上 using namespace __gnu_cxx; ):       [cpp]  #include<iostream>    #include<string>    #include<iterator>    #include<algorithm>    #include<hash_set>    #include<windows.h>       using namespace std;      void main(){          const int N= 3;       int s1[N]={1,2,3};       int s2[N]={4,5,6};       hash_set<int>sa(s1,s1+N);//申明一個集合sa,元素為數組s1        hash_set<int>sb(s2,s2+N);//申明一個集合sb,元素為數組s2        hash_set<int> sc;//申明一個集合sc,為空集合           ostream_iterator<int> output(cout," ");       cout<<"輸出集合sa的元素:";       copy(sa.begin(),sa.end(),output);          cout<<"\n\n輸出集合sb的元素:";       copy(sb.begin(),sb.end(),output);       cout<<endl;          system("pause");      }    

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved