multiset :迭代器的类型:bidirectionaliterator双向迭代器链接
头文件加上:#include<set>
1. 实例化multiset:返回multiset的大小:multisetname.size()
示例程序:
#include <iostream>
#include <set>
using namespace std;
typedef multiset<int> ms;
void print(ms mse);
bool fncomp (int lhs, int rhs)//一元谓词
{
return lhs<rhs;
}
struct classcomp//二元谓词
{
bool operator() (const int& lhs, const int& rhs) const
{
return lhs<rhs;
}
};
int main ()
{
ms mse1;// empty multiset of ints
int myints[]= {10,30,20,40,50};
ms mse2 (myints,myints+5); // range
ms mse3 =mse2; // a copy of second
ms mse4 (mse2.begin(), mse2.end());// iterator ctor.
//ms mse5(5);//不允许先指定multiset的大小
cout<<mse1.size()<<endl;//multisetname.size()返回该容器现在的大小,没有capacity方法
cout<<mse2.size()<<endl;
cout<<mse3.size()<<endl;
cout<<mse4.size()<<endl;
print(mse1);//只能用迭代器访问元素,不支持下标运算
print(mse2);
print(mse3);
print(mse4);
multiset<int,classcomp> mse; // class as Compare构造一个内部按照该一元谓词排序的multiset
bool(*fn_pt)(int,int) = fncomp;
multiset<int,bool(*)(int,int)> sixth (fn_pt); // function pointeras Compare构造一个内部按照该//二元谓词排序的multiset
return 0;
}
void print(ms mse)
{
ms::iterator msed=mse.begin();
for(; msed!=mse.end(); msed++)
{
cout<<*msed<<" ";
}
cout<<endl;
}
2. 迭代器:返回指向开头元素的迭代器:mse1.begin();返回指向实际结尾后面一个元素的迭代器mse1.end(),常用来表示遍历整个multiset:for(;msed !=mse .end();msed ++)。获取元素的值用*。multiset的迭代器不支持+ - 运算
其他不常用的迭代器如下:
Return reverse iterator to reversebeginning (public member function )
Return reverse iterator to reverseend (public member function )
Return const_iterator tobeginning (public member function )
Return const_iterator to end (publicmember function )
Return const_reverse_iterator to reversebeginning (public member function )
Return const_reverse_iterator to reverseend (public member function )
3. 向multiset中插入元素:返回值介绍如下:
In the versions returning a value, this isan iterator pointing to the newly inserted element in the multiset.
示例程序:
#include <iostream>
#include <set>
using namespace std;
typedef multiset<int> ms;
void print(ms mse);
int main()
{
ms mse1;
mse1.insert(3);
mse1.insert(1);
mse1.insert(4);
print(mse1);//默认按照<排序
mse1.insert(mse1.begin(),5);//第一个参数:要插入的位置的迭代器加不加都行,没有什么作用
print(mse1);//结果与不加mse1.begin()一样
int arr[]={1,2,6,-2,-7};
ms mse2(arr,arr+5);
print(mse2);//排完序了
mse1.insert(mse2.begin(),mse2.end());//将另一个multimset容器的从第一个迭代器到第//二个迭代器范围的元素插入到本迭代器中,相同的元素不会被删除(multiset中元素不是互//异的),包括第一个迭代器指向的元素,不包括第二个迭代器指向的元素
print(mse1);
return 0;
}
void print(ms mse)
{
ms::iterator msed=mse.begin();
for(; msed!=mse.end(); msed++)
{
cout<<*msed<<" ";
}
cout<<endl;
}
运行结果:
1 3 4
1 3 4 5
-7 -2 1 2 6
-7 -2 1 1 2 3 4 5 6
4. 删除元素
示例程序:
#include <iostream>
#include <set>
using namespace std;
typedef multiset<int> ms;
void print(ms mse);
int main()
{
ms mse;
for (int i=1; i<=10; i++)
{
mse.insert(i*10);
}
print(mse);
ms::iterator msed=mse.find(20);//msed为指向元素值为20的迭代器
mse.erase(msed);//参数为一个迭代器时,删除该迭代器指向的元素
print(mse);
mse.erase(50);//删除特定值的元素
print(mse);
msed=mse.find(60);
mse.erase(msed,mse.end());//将multimset容器的从第一个迭代器到第二个迭代器范围
//的元素删除,包括第一个迭代器指向的元素,不包括第二个迭代器指向的元素
print(mse);
return 0;
}
void print(ms mse)
{
ms::iterator msed=mse.begin();
for(; msed!=mse.end(); msed++)
{
cout<<*msed<<" ";
}
cout<<endl;
}
运行结果:
10 20 30 40 50 60 70 80 90 100
10 30 40 50 60 70 80 90 100
10 30 40 60 70 80 90 100
10 30 40
5. 清空集合所有内容
示例程序:
#include <set>
using namespace std;
typedef multiset<int> ms;
void print(ms mse);
int main()
{
ms mse;
for (inti=1; i<6; i++)
{
mse.insert(10);
}
print(mse);
mse.clear();
print(mse);
mse.insert(12);
print(mse);
return 0;
}
void print(ms mse)
{
ms::iterator msed=mse.begin();
for(;msed!=mse.end(); msed++)
{
cout<<*msed<<" ";
}
cout<<endl;
}
运行结果:
10 10 10 10 10
12
6. 查找元素:返回指向该元素的迭代器 s::iterator sed=se.find(thespecificvalue);,找不到返回se.end().若有多个相同值的元素,每次找只返回第一个元素的迭代器。
示例程序:
#include <iostream>
#include <set>
using namespace std;
typedef multiset<int> ms;
void print(ms mse);
int main()
{
ms mse;
for (int i=1; i<6; i++)
{
mse.insert(10);
}
print(mse);
mse.erase(mse.find(10));
print(mse);
return 0;
}
void print(ms mse)
{
ms::iterator msed=mse.begin();
for(; msed!=mse.end(); msed++)
{
cout<<*msed<<" ";
}
cout<<endl;
}
运行结果:
10 10 10 10 10
10 10 10 10
若想返回标志相同元素区间的迭代器,使用:equal_range函数
示例程序:
#include <iostream>
#include <set>
using namespace std;
typedef multiset<int> ms;
typedef multiset<int>::iteratorit; // aliasing the iterator type used
int main ()
{
int myints[]= {77,30,16,2,30,30};
ms mymultiset (myints, myints+6); // 2 16 30 30 30 77
pair<it,it> ret = mymultiset.equal_range(30); // ^ ^
mymultiset.erase(ret.first,ret.second);
cout << "mymultiset contains:";
for (it msed=mymultiset.begin(); msed!=mymultiset.end(); ++msed)
{
cout << ' ' << *msed;
}
cout << '\n';
return 0;
}
运行结果:
mymultiset contains: 2 16 77
7. 计算元素的个数:找到就返回实际个数,找不到返回0.
示例程序:
#include <iostream>
#include <set>
using namespace std;
typedef multiset<int> ms;
void print(ms mse);
int main()
{
ms mse;
for (int i=1; i<6; i++)
{
mse.insert(10);
}
print(mse);
int num1=mse.count(10);
int num2=mse.count(1);
cout<<num1<<'\t'<<num2<<endl;
return 0;
}
void print(ms mse)
{
ms::iterator msed=mse.begin();
for(; msed!=mse.end(); msed++)
{
cout<<*msed<<" ";
}
cout<<endl;
}
运行结果:
10 10 10 10 10
5 0
8. ms::iteratormsed=lower_bound(value):返回第一个满足该元素的值>=value的该元素的迭代器
ms::iterator msed=upper_bound(value):返回第一个满足该元素的值>value的该元素的迭代器
示例程序:
#include <iostream>
#include <set>
using namespace std;
typedef multiset<int> ms;
void print(ms mse);
int main()
{
ms mse;
for (int i=1; i<6; i++)
{
mse.insert(i*10);
}
print(mse);
ms::iterator msed1,msed2;
msed1=mse.lower_bound(20);
msed2=mse.upper_bound(30);
cout<<*msed1<<"\t"<<*msed2;
return 0;
}
void print(ms mse)
{
ms::iterator msed=mse.begin();
for(; msed!=mse.end(); msed++)
{
cout<<*msed<<" ";
}
cout<<endl;
}
运行结果:
10 20 30 40 50
20 40
9.