区别:set不允许有重复的值,multiset可以有重复的值。
代码示例
#include <iostream>
#include <queue>
#include <algorithm>
#include <string>
#include <set>
using namespace std;
void print(const set<int>& s)
{
for(set<int>::const_iterator it = s.begin(); it != s.end(); it++)
{
cout<< *it <<" ";
}
cout << endl;
}
//test set for two insert
void test2()
{
set<int>s;
pair<set<int>::iterator,bool> ret = s.insert(10); //插入成功,bool为true
if(ret.second)
{
cout<< "the first insert successed!!" << endl;
}
else
{
cout << "the first insert failed!!" << endl;
}
ret = s.insert(10);
if(ret.second)
{
cout<< "the second insert successed!!" << endl;
}
else
{
cout << "the second insert failed!!" << endl;
}
}
//test multist for two insert
void test()
{
multiset<int> ms;
ms.insert(10);
ms.insert(10);
ms.insert(20);
ms.insert(50);
for(multiset<int>::iterator it = ms.begin(); it != ms.end(); it++)
{
cout<< *it <<" ";
}
cout << endl;
}
int main()
{
test();
test2();
return 0;
}
结果输出