重载运算符-operator
2024-09-03 03:27:01
先看段代码:
struct node
{
friend bool operator< (node n1, node n2){ // 优先取最小的,它与下面的 // 是等价的
return n1.e > n2.e;
}
//bool operator< (const node &r)const{ // <是重载运算符,不是普通的小于
// return s > r.s;
//}
int s, e;
node(int a, int b){
s = a;
e = b;
}
};
//bool operator< (const node& a, const node& b){ // 其也是等价的
// return a.s > b.s;
//} int main() {
priority_queue<node>que; // 此优先队列实现的是取最小的 que.push(node(1, 10));
que.push(node(5, 50));
que.push(node(2, 20));
while(!que.empty()){
printf("%d\n", que.top().s);
que.pop();
}
return 0;
}
在结构体中
struct node
{
int s, e;
node(int _v , int _c ):s(_v),e(_c){} // 这样写是可以进行附初值
// 同理与下面的 // 是等价的
//node(int a, int b){
//s = a;
//e = b;
//}
}; int main() {
queue<node>que; que.push(node(1, 10));
que.push(node(5, 50));
que.push(node(2, 20));
//while(!que.empty()){
//printf("%d\n", que.top().s);
//que.pop();
//}
while(!que.empty()){
printf("%d %d\n", que.front().s, que.front().e);
que.pop();
}
return 0;
}
最新文章
- 【2016-11-7】【坚持学习】【Day22】【Oracle 递归查询】
- MVC Core
- 模板类重载<;<;运算符
- windows下用一台机器配置分布式redis(主从服务器)
- ASP.NET 中OAUTH 2.0 及OPENID CONNECT的介绍
- ExtractNewFolderPath
- Websocket 与代理服务器如何交互? How HTML5 Web Sockets Interact With Proxy Servers
- weiphp 微信公众号用程序来设置指定内容消息回复业务逻辑操作
- shell 编程之 if...else case...esac
- Spring实现无需注解实现自动注入
- hihocoder Challenge 29 D. 不上升序列
- Android使用bindService作为中间人对象开启服务
- Kafka producer介绍
- Unity3D之游戏架构脚本该如何来写
- Problem of Uninstall Cloudera: Cannot Add Hdfs and Reported Cannot Find CDH&#39;s bigtop-detect-javahome
- Java代码度量分析工具:Designite简介
- 使用Git Wiki 管理文档时,文档编写的基本用法
- (LeetCode 72)Edit Distance
- Android开发牛刀小试之“AA算钱软件”开发(一)
- JS高级程序设计第三版——变量、作用域和内存问题