C++ STL 练手(multiset的使用)
2018-02-12 本文已影响56人
863cda997e42
#include <set>
#include <iostream>
using namespace std;
int main()
{
///1. 初始化
multiset<int> num;
multiset<int>::iterator iter,beg,end;
cout << num.max_size() << endl;///multiset容纳上限
cout << endl;
///2. 添加元素
for (int i = 0; i < 10; i++)
num.insert(i);
cout << num.size() << endl;
cout << endl;
///3. 遍历
for (iter = num.begin(); iter != num.end(); iter++)
cout << *iter << " " ;
cout << endl;
cout << endl;
///4. 查询
iter = num.find(1);
if (iter != num.end())
cout << *iter << endl;
else
cout << -1 << endl;
iter = num.find(99);
if (iter != num.end())
cout << *iter << endl;
else
cout << -1 << endl;
cout << endl;
beg=num.lower_bound(2);
end=num.upper_bound(7);
for (; beg != end; beg++)
cout << *beg << " " ;
cout << endl;
///5. 删除
iter = num.find(1);
num.erase(iter);
cout << num.size() << endl;
for (iter = num.begin(); iter != num.end(); iter++)
cout << *iter << " " ;
cout << endl;
cout << endl;
///6. 判空与清空
if (!num.empty())
num.clear();
}