Set
class="c++">/*************************************************************************
> File Name: testSet.cpp
> Author: ma6174
> Mail: ma6174@163.com
> Created Time: 2013年12月 2日 21:17:59
************************************************************************/
#include<iostream>
#include<iterator>
#include<set>
using namespace std;
int main() {
/* set<int> s;
pair<set<int>::iterator,bool> result = s.insert(3);
if(result.second) {
cout<<"insert success!"<<endl;
}
else {
cout<<"insert error"<<endl;
}
*/
typedef set<int,greater<int> > IntSet;
IntSet s1;
s1.insert(4);
s1.insert(3);
s1.insert(5);
s1.insert(1);
s1.insert(6);
s1.insert(2);
s1.insert(5);
IntSet::iterator pos ;
for(pos = s1.begin();pos != s1.end();pos++) {
cout<<*pos <<' ';
}
cout<<endl;
pair<IntSet::iterator,bool> result = s1.insert(4);
if(result.second) {
cout<<"success insert "<<*(result.first)<<endl;
cout<<distance(s1.begin(),result.first)<<endl;
} else {
cout<<" exists"<<endl;
}
set<int> s2(s1.begin(),s1.end());
copy(s2.begin(),s2.end(),ostream_iterator<int>(cout," "));
cout<<endl;
s2.erase(s2.begin(),s2.find(3));
int num ;
num = s2.erase(5);
cout<<num<<" elements removed"<<endl;
copy(s2.begin(),s2.end(),ostream_iterator<int>(cout," "));
cout<<endl;
}
输出:
6 5 4 3 2 1
exists
1 2 3 4 5 6
1 elements removed
3 4 6
Multiset
/*************************************************************************
> File Name: testSet.cpp
> Author: ma6174
> Mail: ma6174@163.com
> Created Time: 2013年12月 2日 21:17:59
************************************************************************/
#include<iostream>
#include<iterator>
#include<set>
using namespace std;
int main() {
/* set<int> s;
pair<set<int>::iterator,bool> result = s.insert(3);
if(result.second) {
cout<<"insert success!"<<endl;
}
else {
cout<<"insert error"<<endl;
}
*/
typedef set<int,greater<int> > IntSet;
IntSet s1;
s1.insert(4);
s1.insert(3);
s1.insert(5);
s1.insert(1);
s1.insert(6);
s1.insert(2);
s1.insert(5);
IntSet::iterator pos ;
for(pos = s1.begin();pos != s1.end();pos++) {
cout<<*pos <<' ';
}
cout<<endl;
pair<IntSet::iterator,bool> result = s1.insert(4);
if(result.second) {
cout<<"success insert "<<*(result.first)<<endl;
cout<<distance(s1.begin(),result.first)<<endl;
} else {
cout<<" exists"<<endl;
}
set<int> s2(s1.begin(),s1.end());
copy(s2.begin(),s2.end(),ostream_iterator<int>(cout," "));
cout<<endl;
s2.erase(s2.begin(),s2.find(3));
int num ;
num = s2.erase(5);
cout<<num<<" elements removed"<<endl;
copy(s2.begin(),s2.end(),ostream_iterator<int>(cout," "));
cout<<endl;
}
输出:
6 5 5 4 3 2 1
1 2 3 4 4 5 5 6
2 elements removed
3 4 4 6