// set multiset map multimap的内部结构是平衡树
// 二叉平衡树的检索使用中序遍历算法
// 对于这些容器中的键值,不可以直接修改
// set默认从小到大
#include<bits/stdc++.h>
using namespace std;
set<int> s;
int main(){
//插入 s.insert(8); 重复元素不会插入
/*遍历 set<int>::iterator it
for(it=s.begin(); it!=s.end(); it++)
*/
/*反向遍历 set<int>::reverse_iterator rit 定义反向迭代器
for(rit=s.rbegin(); rit!=s.rend(); rit++)
rbegin() 返回的值和end()相同
rend() 返回的值和rbegin()相同
*/
/*删除 s.erase(s.begin());
s.erase(s.begin()+1,s.end()-1);
s.erase(8);*/
/*检索 如果找到,返回迭代器的位置;没找到,返回end()
set<int>::iterator it;
it=s.find(8);*/
/*自定义比较函数
1.元素不是结构体,重载"()"运算符
struct myCOMP
{
bool operator()(const int &a, const int &b) //(函数内部的判断是 a<b 看的返回值)
{
if(a!=b) return a>b; //按照键值从大到小排列
else return a>b;
}
}
set<int,myCOMP> s;
2.元素是结构体,直接把比较函数写在结构体内
struct inf
{
string name;
float score;
//重载 < 号,自定义排序规则
bool operator < (const inf &a) const
{
return a.score<b.score; //score由大到小排列;要由小到大,换成>;
}
};
set<inf> s;*/
return 0;
}
STL常用函数复习之————set
最新推荐文章于 2022-10-12 16:51:09 发布