set

<a href="http://blog.csdn.net/lansetiankong_yiyi/article/details/5816362" target=_blank>set</a>  


set/multiset會根據待定的排序準則,自動將元素排序。兩者不同在於前者不允許元素重複,而後者允許。
1) 不能直接改變元素值,因爲那樣會打亂原本正確的順序,要改變元素值必須先刪除舊元素,則插入新元素
2) 不提供直接存取元素的任何操作函數,只能通過迭代器進行間接存取,而且從迭代器角度來看,元素值是常數
3) 元素比較動作只能用於型別相同的容器(即元素和排序準則必須相同)
set模板原型://Key爲元素(鍵值)類型
template <class Key, class Compare=less<Key>, class Alloc=STL_DEFAULT_ALLOCATOR(Key) >
從原型可以看出,可以看出比較函數對象及內存分配器採用的是默認參數,因此如果未指定,它們將採用系統默認方式,
另外,利用原型,可以有效地輔助分析創建對象的幾種方式
*/
#include <iostream>
#include <string>
#include <set>

using namespace std;

struct strLess
{
   bool operator() (const char *s1, const char *s2) const
   {
    return strcmp(s1, s2) < 0;
   }
};

void printSet(set<int> s)
{
copy(s.begin(), s.end(), ostream_iterator<int>(cout, ", ") );

// set<int>::iterator iter;
// for (iter = s.begin(); iter != s.end(); iter++)
//    //cout<<"set["<<iter-s.begin()<<"]="<<*iter<<", "; //Error
//    cout<<*iter<<", ";
cout<<endl;
}

void main()
{
//創建set對象,共5種方式,提示如果比較函數對象及內存分配器未出現,即表示採用的是系統默認方式
//創建空的set對象,元素類型爲int,
set<int> s1; 
//創建空的set對象,元素類型char*,比較函數對象(即排序準則)爲自定義strLess
set<const char*, strLess> s2( strLess); 
//利用set對象s1,拷貝生成set對象s2
set<int> s3(s1); 
//用迭代區間[&first, &last)所指的元素,創建一個set對象
int iArray[] = {13, 32, 19};
set<int> s4(iArray, iArray + 3);
//用迭代區間[&first, &last)所指的元素,及比較函數對象strLess,創建一個set對象
const char* szArray[] = {"hello", "dog", "bird" };
set<const char*, strLess> s5(szArray, szArray + 3, strLess() );

//元素插入:
//1,插入value,返回pair配對對象,可以根據.second判斷是否插入成功。(提示:value不能與set容器內元素重複)
//pair<iterator, bool> insert(value)
//2,在pos位置之前插入value,返回新元素位置,但不一定能插入成功
//iterator insert(&pos, value)
//3,將迭代區間[&first, &last)內所有的元素,插入到set容器
//void insert[&first, &last)
cout<<"s1.insert(...) : "<<endl;
for (int i = 0; i <5 ; i++)
    s1.insert(i*10);
printSet(s1);

cout<<"s1.insert(20).second = "<<endl;;
if (s1.insert(20).second)
    cout<<"Insert OK!"<<endl;
else
    cout<<"Insert Failed!"<<endl;

cout<<"s1.insert(50).second = "<<endl;
if (s1.insert(50).second)
{cout<<"Insert OK!"<<endl; printSet(s1);}
else
    cout<<"Insert Failed!"<<endl;

cout<<"pair<set<int>::iterator::iterator, bool> p;/np = s1.insert(60);/nif (p.second):"<<endl;
pair<set<int>::iterator::iterator, bool> p;
p = s1.insert(60);
if (p.second)
{cout<<"Insert OK!"<<endl; printSet(s1);}
else
   cout<<"Insert Failed!"<<endl;

//元素刪除
//1,size_type erase(value) 移除set容器內元素值爲value的所有元素,返回移除的元素個數
//2,void erase(&pos) 移除pos位置上的元素,無返回值
//3,void erase(&first, &last) 移除迭代區間[&first, &last)內的元素,無返回值
//4,void clear(), 移除set容器內所有元素

cout<<"/ns1.erase(70) = "<<endl;
s1.erase(70);
printSet(s1);
cout<<"s1.erase(60) = "<<endl;
s1.erase(60);
printSet(s1);

cout<<"set<int>::iterator iter = s1.begin();/ns1.erase(iter) = "<<endl;
set<int>::iterator iter = s1.begin();
s1.erase(iter);
printSet(s1);

//元素查找
//count(value)返回set對象內元素值爲value的元素個數
//iterator find(value)返回value所在位置,找不到value將返回end()
//lower_bound(value),upper_bound(value), equal_range(value) 略
cout<<"/ns1.count(10) = "<<s1.count(10)<<", s1.count(80) = "<<s1.count(80)<<endl;
cout<<"s1.find(10) : ";
if (s1.find(10) != s1.end()) 
    cout<<"OK!"<<endl;
else
    cout<<"not found!"<<endl;

cout<<"s1.find(80) : ";
if (s1.find(80) != s1.end()) 
    cout<<"OK!"<<endl;
else
    cout<<"not found!"<<endl;

//其它常用函數
cout<<"/ns1.empty()="<<s1.empty()<<", s1.size()="<<s1.size()<<endl;
set<int> s9;
s9.insert(100);
cout<<"s1.swap(s9) :"<<endl;
s1.swap(s9);
cout<<"s1: "<<endl;
printSet(s1);
cout<<"s9: "<<endl;
printSet(s9);
//lower_bound,upper_bound,equal_range(略)
}


///////////////i測試結果/////////////////////////
s1.insert(...) :
0, 10, 20, 30, 40,
s1.insert(20).second =
Insert Failed!
s1.insert(50).second =
Insert OK!
0, 10, 20, 30, 40, 50,
pair<set<int>::iterator::iterator, bool> p;
p = s1.insert(60);
if (p.second):
Insert OK!
0, 10, 20, 30, 40, 50, 60,

s1.erase(70) =
0, 10, 20, 30, 40, 50, 60,
s1.erase(60) =
0, 10, 20, 30, 40, 50,
set<int>::iterator iter = s1.begin();
s1.erase(iter) =
10, 20, 30, 40, 50,

s1.count(10) = 1, s1.count(80) = 0
s1.find(10) : OK!
s1.find(80) : not found!

s1.empty()=0, s1.size()=5
s1.swap(s9) :
s1:
100,
s9:
10, 20, 30, 40, 50,

發佈了82 篇原創文章 · 獲贊 9 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章