STL常用函数复习之————set

来源:互联网 发布:企业软件开发认证 编辑:程序博客网 时间:2024/05/22 06:56
//setmultisetmapmultimap的内部结构是平衡树//二叉平衡树的检索使用中序遍历算法 //对于这些容器中的键值,不可以直接修改//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;//按照键值从大到小排列elsereturn 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;}