multiset、set、multimap、map 里面的数据是自动排好序的,不用管数据插入|删除的位置,数据改变后依然会自动排序。
set:集合,multiset:排好序的集合
【注意】要访问multiset中的元素必须要使用迭代器(类似于指针)。
指针比较大小比较的是地址的大小。
指针加减整数:
int *p1,*p2;
p1 + n;// 地址p1+ n * sizeof(int);
p1 – p2;//(p1 – p2)/sizeof(int)
https://blog.csdn.net/yanyanwenmeng/article/details/81122946
【注意】st.end() 是指向st中的最后一个元素后面的迭代器(类似于指针)。
#include<iostream>
#include<set>
#include<algorithm>
using namespace std;
int main()
{
multiset<int> st;
int a[10]={1,14,12,13,7,13,21,19,8,8};
for(int t = 0; t < 10; ++t)
{
st.insert(a[t]);
}
multiset<int>::iterator i;//已经定义i迭代器了,后面的i无需定义
for(i = st.begin(); i != st.end(); ++i)//1 7 8 8 12 13 13 14 19 21
cout << *i << " ";
cout << endl;
i = st.find(22);
if(i == st.end())
cout << "not found." << endl;
st.insert(22);//1 7 8 8 12 13 13 14 19 21 22
i = st.find(22);//需要更新i的值
if(i != st.end())
cout << "found:" << *i << endl;
i = st.lower_bound(13);//不能写成st.lower_bound(a,a+10,13)
cout << *i << endl;//13
i = st.upper_bound(13);
cout << *i << endl;//14
i = st.upper_bound(14);
cout << *i << endl;//19
st.erase(14);
for(i = st.begin(); i != st.end(); ++i)//1 7 8 8 12 13 13 19 21 22
cout << *i << " ";
return 0;
}