浏览 1329 次
锁定老帖子 主题:C++ Set vs Multiset
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2013-12-02
<pre name="code" 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; }</pre> 输出: 6 5 4 3 2 1 exists 1 2 3 4 5 6 1 elements removed 3 4 6 Multiset <pre name="code" 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; }</pre> 输出: 6 5 5 4 3 2 1 1 2 3 4 4 5 5 6 2 elements removed 3 4 4 6 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |