C++ Set vs Multiset_C/C++_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > C/C++ > C++ Set vs Multiset

C++ Set vs Multiset

 2013/12/3 0:26:04  zhangyan19870108  程序员俱乐部  我要评论(0)
  • 摘要:Set/*************************************************************************>FileName:testSet.cpp>Author:ma6174>Mail:ma6174@163.com>CreatedTime:2013年12月2日21:17
  • 标签:c++
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
上一篇: 类的继承总结 下一篇: 没有下一篇了!
发表评论
用户名: 匿名