set中常用的方法
set中常用的方法
begin()???? ,返回set容器的第一個元素
end() ,返回set容器的最后一個元素
clear()?? ??? ?,刪除set容器中的所有的元素
empty() ,判斷set容器是否為空
max_size() ,返回set容器可能包含的元素最大個數
size() ,返回當前set容器中的元素個數
rbegin ,返回的值和end()相同
rend() ,返回的值和rbegin()相同
寫一個程序練一練這幾個簡單操作吧:?
View Code 1 #include <iostream> 2 #include <set> 3 4 using namespace std; 5 6 int main() 7 { 8 set<int> s; 9 s.insert(1); 10 s.insert(2); 11 s.insert(3); 12 s.insert(1); 13 cout<<"set 的 size 值為 :"<<s.size()<<endl; 14 cout<<"set 的 maxsize的值為 :"<<s.max_size()<<endl; 15 cout<<"set 中的第一個元素是 :"<<*s.begin()<<endl; 16 cout<<"set 中的最后一個元素是:"<<*s.end()<<endl; 17 s.clear(); 18 if(s.empty()) 19 { 20 cout<<"set 為空 !!!"<<endl; 21 } 22 cout<<"set 的 size 值為 :"<<s.size()<<endl; 23 cout<<"set 的 maxsize的值為 :"<<s.max_size()<<endl; 24 return 0; 25 }運行結果:
小結:插入3之后雖然插入了一個1,但是我們發現set中最后一個值仍然是3哈,這就是set 。還要注意begin() 和 end()函數是不檢查set是否為空的,使用前最好使用empty()檢驗一下set是否為空.
?
count()?用來查找set中某個某個鍵值出現的次數。這個函數在set并不是很實用,因為一個鍵值在set只可能出現0或1次,這樣就變成了判斷某一鍵值是否在set出現過了。
示例代碼:
View Code 1 #include <iostream> 2 #include <set> 3 4 using namespace std; 5 6 int main() 7 { 8 set<int> s; 9 s.insert(1); 10 s.insert(2); 11 s.insert(3); 12 s.insert(1); 13 cout<<"set 中 1 出現的次數是 :"<<s.count(1)<<endl; 14 cout<<"set 中 4 出現的次數是 :"<<s.count(4)<<endl; 15 return 0; 16 }運行結果:
?
equal_range()?,返回一對定位器,分別表示第一個大于或等于給定關鍵值的元素和?第一個大于給定關鍵值的元素,這個返回值是一個pair類型,如果這一對定位器中哪個返回失敗,就會等于end()的值。具體這個有什么用途我還沒遇到過~~~
示例代碼:
View Code 1 #include <iostream> 2 #include <set> 3 4 using namespace std; 5 6 int main() 7 { 8 set<int> s; 9 set<int>::iterator iter; 10 for(int i = 1 ; i <= 5; ++i) 11 { 12 s.insert(i); 13 } 14 for(iter = s.begin() ; iter != s.end() ; ++iter) 15 { 16 cout<<*iter<<" "; 17 } 18 cout<<endl; 19 pair<set<int>::const_iterator,set<int>::const_iterator> pr; 20 pr = s.equal_range(3); 21 cout<<"第一個大于等于 3 的數是 :"<<*pr.first<<endl; 22 cout<<"第一個大于 3的數是 : "<<*pr.second<<endl; 23 return 0; 24 }運行結果:
?
erase(iterator)? ,刪除定位器iterator指向的值
erase(first,second),刪除定位器first和second之間的值
erase(key_value),刪除鍵值key_value的值
看看程序吧:
View Code 1 #include <iostream> 2 #include <set> 3 4 using namespace std; 5 6 int main() 7 { 8 set<int> s; 9 set<int>::const_iterator iter; 10 set<int>::iterator first; 11 set<int>::iterator second; 12 for(int i = 1 ; i <= 10 ; ++i) 13 { 14 s.insert(i); 15 } 16 //第一種刪除 17 s.erase(s.begin()); 18 //第二種刪除 19 first = s.begin(); 20 second = s.begin(); 21 second++; 22 second++; 23 s.erase(first,second); 24 //第三種刪除 25 s.erase(8); 26 cout<<"刪除后 set 中元素是 :"; 27 for(iter = s.begin() ; iter != s.end() ; ++iter) 28 { 29 cout<<*iter<<" "; 30 } 31 cout<<endl; 32 return 0; 33 }運行結果:
小結:set中的刪除操作是不進行任何的錯誤檢查的,比如定位器的是否合法等等,所以用的時候自己一定要注意。
?
find()??,返回給定值值得定位器,如果沒找到則返回end()。
示例代碼:
View Code 1 #include <iostream> 2 #include <set> 3 4 using namespace std; 5 6 int main() 7 { 8 int a[] = {1,2,3}; 9 set<int> s(a,a+3); 10 set<int>::iterator iter; 11 if((iter = s.find(2)) != s.end()) 12 { 13 cout<<*iter<<endl; 14 } 15 return 0; 16 }?
insert(key_value);?將key_value插入到set中?,返回值是pair<set<int>::iterator,bool>,bool標志著插入是否成功,而iterator代表插入的位置,若key_value已經在set中,則iterator表示的key_value在set中的位置。
inset(first,second);將定位器first到second之間的元素插入到set中,返回值是void.
示例代碼:
View Code 1 #include <iostream> 2 #include <set> 3 4 using namespace std; 5 6 int main() 7 { 8 int a[] = {1,2,3}; 9 set<int> s; 10 set<int>::iterator iter; 11 s.insert(a,a+3); 12 for(iter = s.begin() ; iter != s.end() ; ++iter) 13 { 14 cout<<*iter<<" "; 15 } 16 cout<<endl; 17 pair<set<int>::iterator,bool> pr; 18 pr = s.insert(5); 19 if(pr.second) 20 { 21 cout<<*pr.first<<endl; 22 } 23 return 0; 24 }運行結果:
?
lower_bound(key_value)?,返回第一個大于等于key_value的定位器
upper_bound(key_value),返回最后一個大于等于key_value的定位器
示例代碼:
View Code 1 #include <iostream> 2 #include <set> 3 4 using namespace std; 5 6 int main() 7 { 8 set<int> s; 9 s.insert(1); 10 s.insert(3); 11 s.insert(4); 12 cout<<*s.lower_bound(2)<<endl; 13 cout<<*s.lower_bound(3)<<endl; 14 cout<<*s.upper_bound(3)<<endl; 15 return 0; 16 }運行結果:
總結
- 上一篇: set和multiset容器
- 下一篇: string实现