C++关联容器 Multiset and set

/******************set*/
#include
<iostream>
#include
<set>
#include
<algorithm>
#include
<iterator>
using namespace std;
typedef
set<double,less<double> > DoubleSet;//注意> >间距
int main()
{
const int SIZE=5;
double a[SIZE] = {2.1,4.2,9.5,2.1,3.7};
DoubleSet dd(a,a
+SIZE);
ostream_iterator
<double> output(cout," ");
cout
<<"Double Set Contains: ";
copy(dd.begin(),dd.end(),output);

pair
<DoubleSet::const_iterator,bool> p; //定义一个pair对象,由DoubleSet的const_iterator和一个布尔值组成
p=dd.insert(13.8); //因为容器没有13.8,13.8被插入,则p.first指向13.8,p.second返回为真
cout<<"\n\n"<<*(p.first)<<(p.second?" was ":" was not ")<<" insertd";

cout
<<"\nDoubleset contains:";
copy(dd.begin(),dd.end(),output);
p
=dd.insert(9.5);//因为set关联容器不允许重复,9.5已经存在,故没有被插入,p.second返回为假
cout<<"\n\n"<<*(p.first)<<(p.second?" was ":" was not ")<<" insertd";

cout
<<"\nDoubleset contains:";
copy(dd.begin(),dd.end(),output);
cout
<<endl;

}

muntiset

#include <iostream>
#include
<set>
#include
<algorithm>
#include
<iterator>
using namespace std;
typedef multiset
<int,less<int> > Ims;//注意> >间距
int main()
{
const int SIZE = 10;
int a[SIZE]={7,22,9,1,18,30,100,22,85,13};
Ims intMultiset;
ostream_iterator
<int> output(cout," ");
cout
<<"There are Curently"<<intMultiset.count(15)<<"values of 15 in the multiset\n\n";
intMultiset.insert(
15);
intMultiset.insert(
15);
cout
<<"\nAfter insert,there are "<<intMultiset.count(15)<<" values of 15 in the multiset\n\n";

Ims::const_iterator result;
result
=intMultiset.find(15);

if(result!=intMultiset.end())
cout
<<"Fond values 15\n";
result
=intMultiset.find(20);
if(result==intMultiset.end())
cout
<<"Did not Find values 20\n";
intMultiset.insert(a,a
+SIZE);
cout
<<"\nAfter insert,int multiset contains:\n";
copy(intMultiset.begin(),intMultiset.end(),output);
cout
<<"\n\nLower bound of 22: "<<*(intMultiset.lower_bound(22));
cout
<<"\n\nUpper bound of 22: "<<*(intMultiset.upper_bound(22));

pair
<Ims::const_iterator,Ims::const_iterator> p;//p包含两个成员first,second
p=intMultiset.equal_range(22);//这里使用equal_range函数确定值22在intmultiset中最早出现的位置和最后一次出现的位置
cout<<"\n\nequal_range of 22 :"<<"\n Lower bound: "
<<*(p.first)<<"\n Upper bound"<<*(p.second);
cout
<<endl;
return 0;
}

编辑器加载中...

你可能感兴趣的:(C++)