作者在 2008-08-17 14:26:15 发布以下内容
#include<iostream>
#include<set>
using namespace std;
int main(void)
{
set<int> s1;
int arr[] = {0,0,0,0,1,1,2,3};
set<int> s2(arr,arr+8); //construct from a range
set<int> s3(s2);
set<int>::iterator Iter;
set<int>::reverse_iterator RevIter;
if (s1.empty()) cout<<"set s1 is empty."<<endl;
else cout<<"set s1 is not empty."<<endl;
cout<<"s2(using begin,end)is ";
for (Iter = s2.begin();Iter != s2.end();Iter++) cout<<*Iter<<" ";
cout<<endl;
cout<<"s2(using rbegin,rend)is ";
for (RevIter = s2.rbegin();RevIter != s2.rend();RevIter++) cout<<*RevIter<<" ";
cout<<endl;
s1.insert(arr,arr+4);
pair<set<int>::iterator,bool> Pair;
Pair = s1.insert(0);
if (Pair.second == true) cout<<"element 0 was inserted in s1 successfully"<<endl;
else cout<<"element 0 already exists in s1 and *(pair.first) = "<<*Pair.first<<endl;
set<int>::const_iterator constIter = s1.find(3);
if (constIter != s1.end()) cout<<"s1 contains element 3,*constIter = "<<*constIter<<endl;
cout<<"maxSize/size is "<<s1.max_size()<<"/"<<s1.size()<<endl;
s1.insert(4);
s2.swap(s1);
s1.clear();
cout<<"After calling the clear,size of s1 is "<<s1.size()<<endl;
set<int>::allocator_type Alloc = s1.get_allocator(); //获取S1的内在分配器
set<int>::key_compare Key = s2.key_comp(); //使用key_campare进行s1的键值检验
bool result = Key(2,3);
if (result == true) cout<<"key is function object used by s2.key(2,3) = true"<<endl;
else cout<<"key is a function object used by s2.key(2,3) = false"<<endl;
set<int>::value_compare vc = s2.value_comp(); //使用value_compare进行S2的键值检验
result = vc(10,4);
if (result == true) cout<<"vc is function object used by s2.vc(2,3) = true"<<endl;
else cout<<"vc is a function object used by s2.vc(2,3) = false"<<endl;
cout<<"*(s2.upper_bound(3)) = "<<*(s2.upper_bound(3))<<endl; //返回S2当前值的最近增值迭代器
cout<<"*(s2.lower_bound(3)) = "<<*(s2.lower_bound(3))<<endl; //返回S2当前值的最近增值迭代器
//使用equal_range返回当前元素的上下两个迭代器
pair<set<int>::const_iterator,set<int>::const_iterator> pr = s2.equal_range(3);
cout<<"*pr.first = "<<*pr.first<<"\t"<<"*pr.second = "<<*pr.second<<endl;
//使用erase进行元素的删除
if (s3.erase(1) != 0) cout<<"s3 does not contain 1 any more"<<endl;
else cout<<"No elements in s3 match key 1"<<endl;
//if((s2.erase(s2.begin())) != s2.end()) cout<<"s2 does not contain 0 any more"<<endl;
//else cout<<"No elements in s2 match key 0"<<endl;
s3.erase(s3.begin(),s3.end());
cout<<"After {s3.erase(s3.begin(),s3.end());} s3.size = "<<s3.size()<<endl;
return 0;
}
#include<set>
using namespace std;
int main(void)
{
set<int> s1;
int arr[] = {0,0,0,0,1,1,2,3};
set<int> s2(arr,arr+8); //construct from a range
set<int> s3(s2);
set<int>::iterator Iter;
set<int>::reverse_iterator RevIter;
if (s1.empty()) cout<<"set s1 is empty."<<endl;
else cout<<"set s1 is not empty."<<endl;
cout<<"s2(using begin,end)is ";
for (Iter = s2.begin();Iter != s2.end();Iter++) cout<<*Iter<<" ";
cout<<endl;
cout<<"s2(using rbegin,rend)is ";
for (RevIter = s2.rbegin();RevIter != s2.rend();RevIter++) cout<<*RevIter<<" ";
cout<<endl;
s1.insert(arr,arr+4);
pair<set<int>::iterator,bool> Pair;
Pair = s1.insert(0);
if (Pair.second == true) cout<<"element 0 was inserted in s1 successfully"<<endl;
else cout<<"element 0 already exists in s1 and *(pair.first) = "<<*Pair.first<<endl;
set<int>::const_iterator constIter = s1.find(3);
if (constIter != s1.end()) cout<<"s1 contains element 3,*constIter = "<<*constIter<<endl;
cout<<"maxSize/size is "<<s1.max_size()<<"/"<<s1.size()<<endl;
s1.insert(4);
s2.swap(s1);
s1.clear();
cout<<"After calling the clear,size of s1 is "<<s1.size()<<endl;
set<int>::allocator_type Alloc = s1.get_allocator(); //获取S1的内在分配器
set<int>::key_compare Key = s2.key_comp(); //使用key_campare进行s1的键值检验
bool result = Key(2,3);
if (result == true) cout<<"key is function object used by s2.key(2,3) = true"<<endl;
else cout<<"key is a function object used by s2.key(2,3) = false"<<endl;
set<int>::value_compare vc = s2.value_comp(); //使用value_compare进行S2的键值检验
result = vc(10,4);
if (result == true) cout<<"vc is function object used by s2.vc(2,3) = true"<<endl;
else cout<<"vc is a function object used by s2.vc(2,3) = false"<<endl;
cout<<"*(s2.upper_bound(3)) = "<<*(s2.upper_bound(3))<<endl; //返回S2当前值的最近增值迭代器
cout<<"*(s2.lower_bound(3)) = "<<*(s2.lower_bound(3))<<endl; //返回S2当前值的最近增值迭代器
//使用equal_range返回当前元素的上下两个迭代器
pair<set<int>::const_iterator,set<int>::const_iterator> pr = s2.equal_range(3);
cout<<"*pr.first = "<<*pr.first<<"\t"<<"*pr.second = "<<*pr.second<<endl;
//使用erase进行元素的删除
if (s3.erase(1) != 0) cout<<"s3 does not contain 1 any more"<<endl;
else cout<<"No elements in s3 match key 1"<<endl;
//if((s2.erase(s2.begin())) != s2.end()) cout<<"s2 does not contain 0 any more"<<endl;
//else cout<<"No elements in s2 match key 0"<<endl;
s3.erase(s3.begin(),s3.end());
cout<<"After {s3.erase(s3.begin(),s3.end());} s3.size = "<<s3.size()<<endl;
return 0;
}