set集合:
所有的元素都会在插入的时候自动被排序
set不允许有重复的元素,但是multiset允许容器中有重复的元素
#include <iostream>
#include <set>
using namespace std;
void show(set<int> value)
{
set<int>::iterator setBegin = value.begin();
set<int>::iterator setEnd = value.end();
while (setBegin!=setEnd)
{
cout << *setBegin << ",";
setBegin++;
}
cout << endl << "-----" << endl;
}
void show1(multiset<int> value)
{
multiset<int>::iterator setBegin = value.begin();
multiset<int>::iterator setEnd = value.end();
while (setBegin != setEnd)
{
cout << *setBegin << ",";
setBegin++;
}
cout << endl << "-----" << endl;
}
int main()
{
set<int> s1;
s1.insert(4);
s1.insert(3);
s1.insert(2);
s1.insert(1);
show(s1);
//判断是否为空 empty()
//删除
s1.erase(2);
show(s1);
//查找元素,返回迭代器对象,如果不等于啊end(),则表示找到了元素
set<int>::iterator pos = s1.find(4);
if (pos!=s1.end())
{
cout << *pos << endl;
}
//允许插入重复数据
multiset<int> mset;
mset.insert(1);
mset.insert(1);
show1(mset);
}