C++中的sort函数默认是将元素升序排列的,而priority_queue默认是将元素降序排列的(默认实现的是大顶堆)。

自定义运算符用的比较多,以下2种对sort和priority_queue运算符的重载都有效,效果都是一样的:

 #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std; //sort实现的都是先按a值降序排列,a相同时,按b值降序排列
//priority_queue和sort的默认排序相反,实现的都是先按a值升序排列,a相同时,按b值升序排列 //重载方式一:
struct Node {
int a, b;
Node(int x, int y) {
a = x;
b = y;
}
bool operator < (const Node &c) const {
if(a == c.a) return b > c.b;
return a > c.a;
}
}; //重载方式二:注意要有public
// class Node {
// public:
// int a, b;
// Node(int x, int y) {
// a = x;
// b = y;
// }
// bool operator < (const Node &c) const {
// if(a == c.a) return b > c.b;
// return a > c.a;
// }
// };
int main() {
vector<Node> v;
v.push_back(Node(,));
v.push_back(Node(,));
v.push_back(Node(,));
v.push_back(Node(,));
sort(v.begin(), v.end());
for(int i = ; i < v.size(); ++i) {
cout<<v[i].a<<" "<<v[i].b<<endl;
}
cout<<endl; priority_queue<Node> pq;
pq.push(Node(,));
pq.push(Node(,));
pq.push(Node(,));
pq.push(Node(,));
while(!pq.empty()) {
cout<<pq.top().a<<" "<<pq.top().b<<endl;
pq.pop();
}
return ;
}

输出


对sort单独自定义运算符:

 #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std; //sort实现的都是先按a值降序排列,a相同时,按b值降序排列
//priority_queue和sort的默认排序相反,实现的都是先按a值升序排列,a相同时,按b值升序排列 struct Node {
int a, b;
Node(int x, int y) {
a = x;
b = y;
}
};
/*
//重载方式一:
struct compare {
bool operator()(const Node a, const Node b) {
if(a.a == b.a) return a.b > b.b;
return a.a > b.a;
}
} cmp;
*/ /*
//重载方式二:
bool cmp(const Node a, const Node b) {
if(a.a == b.a) return a.b > b.b;
return a.a > b.a;
}
*/ int main() { vector<Node> v;
v.push_back(Node(,));
v.push_back(Node(,));
v.push_back(Node(,));
v.push_back(Node(,));
//重载方式一和二:
sort(v.begin(), v.end(), cmp);
//重载方式三:
sort(v.begin(), v.end(), [](const Node a, const Node b) {
if(a.a == b.a) return a.b > b.b;
return a.a > b.a;
});
for(int i = ; i < v.size(); ++i) {
cout<<v[i].a<<" "<<v[i].b<<endl;
}
cout<<endl;
return ;
}

输出都是


对priority_queue单独自定义运算符:

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std; //sort实现的都是先按a值降序排列,a相同时,按b值降序排列
//priority_queue和sort的默认排序相反,实现的都是先按a值升序排列,a相同时,按b值升序排列 struct Node {
int a, b;
Node(int x, int y) {
a = x;
b = y;
}
}; struct cmp {
bool operator()(const Node a, const Node b) {
if(a.a == b.a) return a.b > b.b;
return a.a > b.a;
}
}; int main() {
priority_queue<Node, vector<Node>, cmp> pq; pq.push(Node(,));
pq.push(Node(,));
pq.push(Node(,));
pq.push(Node(,));
while(!pq.empty()) {
cout<<pq.top().a<<" "<<pq.top().b<<endl;
pq.pop();
} return ;
}

效果:


C++关于sort和priority_queue的运算符重载的更多相关文章

  1. priority_queue的运算符重载问题

    对于需要比较的函数或STL(最常见的为sort,priority_queue) 要对自创的结构进行运算符重载(sort可以写cmp,一样的效果) 1.只能对小于号重载 cmp函数与其起到相同的作用 2 ...

  2. set/priority_queue的运算符重载

    #include<bits/stdc++.h> using namespace std; struct cmp { bool operator ()(int a, int b) //重载小 ...

  3. 运算符重载 与 sort()

    运算符重载与sort() 二话不说上代码: #include <iostream> #include <algorithm> using namespace std; stru ...

  4. C++运算符重载的妙用

    运算符重载(Operator overloading)是C++重要特性之中的一个,本文通过列举标准库中的运算符重载实例,展示运算符重载在C++里的妙用.详细包含重载operator<<,o ...

  5. 深入C++05:运算符重载

    运算符重载 1.复数类 运算符重载目的:使对象运算表现得和编译器内置类型一样: 复数类例子 #include<iostream> using namespace std; class CC ...

  6. C++ 运算符重载时,将运算符两边对象交换问题.

    在C++进行运算符重载时, 一般来讲,运算符两边的对象的顺序是不能交换的. 比如下面的例子: #include <iostream> using namespace std; class ...

  7. C#高级编程笔记2016年10月12日 运算符重载

    1.运算符重载:运算符重重载的关键是在对象上不能总是只调用方法或属性,有时还需要做一些其他工作,例如,对数值进行相加.相乘或逻辑操作等.例如,语句if(a==b).对于类,这个语句在默认状态下会比较引 ...

  8. C++运算符重载

    C++运算符重载 基本知识 重载的运算符是具有特殊名字的函数,他们的名字由关键字operator和其后要定义的运算符号共同组成. 运算符可以重载为成员函数和非成员函数.当一个重载的运算符是成员函数时, ...

  9. 标准C++之运算符重载和虚表指针

    1 -> *运算符重载 //autoptr.cpp     #include<iostream> #include<string> using namespace std ...

随机推荐

  1. Jack Straws(poj 1127) 两直线是否相交模板

    http://poj.org/problem?id=1127   Description In the game of Jack Straws, a number of plastic or wood ...

  2. python 开发学习

    https://www.cnblogs.com/wj-1314/p/8476197.html

  3. RxSwift学习笔记2:Observable/生命周期/Event/oneNext/onError/onCompleted/

     Observable 是 Rx 的根基 官网:http://reactivex.io/ github地址:https://github.com/ReactiveX/RxSwift Observabl ...

  4. Android x86模拟器Intel Atom x86 System Image配置与使用方法

    Android x86模拟器Intel Atom x86 System Image配置与使用方法      前言:      大家现在开发使用的Android 模拟器模拟的是 ARM 的体系结构(ar ...

  5. Convolutional Restricted Boltzmann Machines

    参考论文:1.Stacks of Convolutional Restricted Boltzmann Machines for Shift-Invariant Feature Learning   ...

  6. 【转】jQuery.validate 用法

    名称                              返回类型                描述 validate(options)          返回:Validator       ...

  7. collaborative filtering协同过滤

    每次我想看电影的时候,都会去问我的朋友,小健.一般他推荐的电影,我都比较喜欢.显然不是所有人都有小健这样的能力.因为我碰巧和小健有类似的品味. 这个生活中的经验,实际上有着广泛的用途. 当系统需要为某 ...

  8. python 操作mysql数据库之模拟购物系统登录及购物

    python 操作mysql数据库之模拟购物系统登录及购物,功能包含普通用户.管理员登录,查看商品.购买商品.添加商品,用户充值等. mysql 数据库shop 表结构创建如下: create TAB ...

  9. FFmpeg4.0笔记:rtsp2rtmp

    Github https://github.com/gongluck/FFmpeg4.0-study.git #include <iostream> using namespace std ...

  10. .net core 与ELK(3)安装Kibana

    1.去产品官网下载https://www.elastic.co/downloads/kibana 对应的tar.gz的压缩包,放到/usr/local/src目录 2.解压 -linux-x86_64 ...