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. Scala深入浅出实战经典:29,Case class和Case object代码实战解析

    今天学习了王家林老师scala讲座的第29讲,case class和case object的应用实战.做下记录. 信息来源于 DT大数据梦工厂微信公众账号:DT_Spark 关注微信账号,获取更多关于 ...

  2. poj1573模拟

    Robot Motion Time Limit: 1000 MS Memory Limit: 10000 KB 64-bit integer IO format: %I64d , %I64u Java ...

  3. 6.python3爬虫之urllib库

    # 导入urllib.request import urllib.request # 向指定的url发送请求,并返回服务器响应的类文件对象 response = urllib.request.urlo ...

  4. 转 iOS宏定义的使用与规范

    宏定义在很多方面都会使用,例如定义高度.判断iOS系统.工具类,还有诸如文件路径.服务端api接口文档.为了对宏能够快速定位和了解其功能,我们最好在定义的时候将其放入特定的头文件中,下面我抛砖引玉,对 ...

  5. [Proposal]MyTools

    [名称]:MyTools [需求分析]:现在市场上常用的移动端工具类APP,要么功能单一,如手电筒,录音机,指南针等,要么虽然有多种功能的整合,但只是单一的堆砌,内部依然是一个个独立的功能模块,并未形 ...

  6. C#基础复习(4) 之 浅析List、Dictionary

    参考资料 [1] .netCore 源码 https://github.com/dotnet/corefx [2] <Unity 3D脚本编程 使用C#语言开发跨平台游戏>陈嘉栋著 [3] ...

  7. .net core 分布式配置中心

    github地址 https://github.com/wangchengqun/ratel 配置文件 数据同步端口 Server: ip: 127.0.0.1 port: 7890 浏览器访问 ht ...

  8. 【C#】简单的发送socket字符串

    1 打开VS,新建一个C#窗口程序 2 添加按钮 3 写按钮的事件代码 双击这个按钮 进入代码界面 输入如下内容,注意IP和端口 private void button1_Click(object s ...

  9. awk的匹配

    关系运算符 含义 用法示例 < 小于 x < y > 大于 x > y

  10. ssh远程连接不上linux

    远程连接工具是:Xmanager Enterprise 5-->Xshell linux 发行版本是:CentOS-6.3-x86_64 问题:ssh一直都可以远程连接上linux,一段时间后突 ...