STL set,map , priority_queue 及 lower_bound 的排序比较函数



代码:(结构体外部重构Cmp函数)

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;


struct cmp{
	bool operator ()(const int &a,const int &b)const{
		return aa;
set::iterator it;


map b;


priority_queue ,cmp> q; //在优先队列里 定义顺序和内部顺序相反。
int x[110];
int main(){
	
	a.insert(1);
	a.insert(2);
	a.insert(4);
	it=a.lower_bound(1);
	//cout<<*it<(1,1));
	b.insert(pair(2,2));
	//cout<first;
	
	
	x[0]=1,x[1]=2,x[2]=4;x[3]=-1;
	cout<<*lower_bound(x,x+3,3,cmp());//cmp函数如果数组是递增序列,cmp函数里需要定义为a

代码:(结构体内部重构<比较)

#include 
#include 
#include 
#define mem(a,b) memset(a,b,sizeof(a))
#define ll long long
using namespace std;
struct node{
    int k;
    bool operator < (const  node &a)const{
        return k>a.k;
    }

}a1,a2;

set s;
map m;
int main()
{
    a1.k=2;a2.k=3;
    s.clear();
    s.insert(a1);
    s.insert(a2);
    cout<<(*s.begin()).k<




你可能感兴趣的:(STL)