重载

· · 个人记录

1. 语法区别

2. 为什么会导致结果不同?

2.1. 传值 VS 传引用

2.2. 对于优先队列(priority_queue)的影响

priority_queue 在内部会频繁调用 < 运算符来维护堆结构。如果你的 node 结构体比较大,或者有指针成员、资源管理等,传值会导致:

2.3. 具体到你的代码

你的 node 结构体如下:

struct node{
    int w, pos;
    bool operator<(const node& x) const {
        return w < x.w;
    }
};

2.4. STL 容器的要求

STL 容器(如 priority_queue、set、map)强烈建议比较运算符用常量引用,否则可能导致:

3. 结论

4. 参考资料

5. 总结

优先队列、set、map等容器的比较运算符,必须用const T&作为参数,否则可能导致性能下降甚至结果错误。

P3545 [POI 2012] HUR-Warehouse Store

题目描述

现在有 n 天。第 i 天上午会进货 A_i 件商品,中午的时候会有顾客需要购买 B_i 件商品,可以选择满足顾客的要求,或是无视掉他。

如果要满足顾客的需求,就必须要有足够的库存。问最多能够满足多少个顾客的需求。

输入格式

第一行包含一个整数 n,表示有 n 天。

第二行有 n 个整数 a_i,表示第 i 天上午进货 a 件商品。

第三行包含 n 个整数 b_i,表示在第 i 天中午有顾客来买 b 件商品。

输出格式

第一行一个整数,表示最多能满足几天中顾客的需求。

第二行输出满足这么哪些天顾客的需求。

输入输出样例 #1

输入 #1

6
2 2 1 2 1 0
1 2 2 3 4 4

输出 #1

3
1 2 4

说明/提示

对于 100\% 的数据,1\leqslant n\leqslant 2.5\times 10^5,0\leqslant a_i,b_i \leqslant 10^9。

1. 直觉与常规

2. 这道题的特殊性

2.1 你的结构体定义

struct node{
    int w, pos;
    bool operator<(const node x) const { // 传值
        return w < x.w;
    }
};

或

struct node{
    int w, pos;
    bool operator<(const node& x) const { // 传引用
        return w < x.w;
    }
};

2.2 你用的优先队列

priority_queue<node>

默认是大根堆,即operator<返回true时,x在w更大时优先级更高。

3. 关键点:const修饰位置不同导致的优先队列行为不同

3.1 你的代码实际写法

错误写法(传引用):

bool operator<(const node& x) const {
    return w < x.w;
}

正确写法(传值):

bool operator<(const node x) const {
    return w < x.w;
}

3.2 这两种写法的本质区别

3.3 这道题的特殊点

4. 结论

4.1 为什么会这样?

4.2 如何写最保险?

5. 参考资料

6. 总结

在这道题中,operator<(const node x) const之所以对,而operator<(const node& x) const错,是因为OJ的标准库实现priority_queue时,底层调用的是传值版本的operator<,而不是传引用版本。你只写了传引用版本时,优先队列找不到合适的比较函数,导致用默认的比较(比如按地址),结果就错了。

建议

如需更深入的源码分析或实验代码,欢迎继续追问!

1. 这两个 < 和 const 的作用

1.1 operator<

bool operator<(const node& x) const

1.2 const

bool operator<(const node& x) const

2. 能不能改?

2.1 < 能不能改?

2.2 const 能不能去掉?

3. 对 return 的内容有影响吗?

4. 总结

建议

写比较运算符时,标准写法如下:

bool operator<(const node& x) const {
    return w < x.w;
}

这样最安全、最通用,适用于所有 STL 容器。

如需更深入的解释或例子,欢迎继续追问!

1. 写法示例

1.1 定义cmp函数

struct node {
    int w, pos;
};

bool cmp(const node& a, const node& b) {
    return a.w < b.w; // 按w从小到大排序
}

1.2 用于sort

vector<node> v;
// ...填充v
sort(v.begin(), v.end(), cmp);

1.3 用于priority_queue

priority_queue默认是大根堆(最大值优先),如果你想用自定义比较器,可以这样写:

方式一:用函数指针

priority_queue<node, vector<node>, decltype(&cmp)> q(cmp);

方式二:用仿函数(推荐)

struct cmp {
    bool operator()(const node& a, const node& b) const {
        return a.w > b.w; // 小根堆(w小的优先)
    }
};
priority_queue<node, vector<node>, cmp> q;

注意:priority_queue的比较器是“谁应该排在后面”,所以如果你想让w小的优先,应该写a.w > b.w。

方式三:用lambda(C++11及以上)

auto cmp = [](const node& a, const node& b) {
    return a.w > b.w; // 小根堆
};
priority_queue<node, vector<node>, decltype(cmp)> q(cmp);

2. 适用场景

3. 优点

4. 总结

示例代码

#include <queue>
#include <vector>
#include <iostream>
using namespace std;

struct node {
    int w, pos;
};

struct cmp {
    bool operator()(const node& a, const node& b) const {
        return a.w > b.w; // 小根堆
    }
};

int main() {
    priority_queue<node, vector<node>, cmp> q;
    q.push({3, 1});
    q.push({1, 2});
    q.push({2, 3});
    while (!q.empty()) {
        cout << q.top().w << " " << q.top().pos << endl;
        q.pop();
    }
    return 0;
}

输出:

1 2
2 3
3 1

如需具体到你这道题的完整代码示例,也可以告诉我!

1. priority_queue 用自定义比较器

#include <queue>
#include <vector>

struct node {
    int w, pos;
};

struct cmp {
    bool operator()(const node& a, const node& b) const {
        return a.w > b.w; // 小根堆,w小的优先
    }
};

std::priority_queue<node, std::vector<node>, cmp> q;

2. set 用自定义比较器

#include <set>

struct node {
    int w, pos;
};

struct cmp {
    bool operator()(const node& a, const node& b) const {
        return a.w < b.w; // 按w从小到大排序
    }
};

std::set<node, cmp> s;

3. map 用自定义比较器

#include <map>

struct node {
    int w, pos;
};

struct cmp {
    bool operator()(const node& a, const node& b) const {
        return a.w < b.w; // 按w从小到大排序
    }
};

std::map<node, int, cmp> m;

4. lambda写法(C++11及以上)

如果你用的是 C++11 及以上,可以用 lambda 表达式作为比较器:

priority_queue 用 lambda

auto cmp = [](const node& a, const node& b) {
    return a.w > b.w;
};
std::priority_queue<node, std::vector<node>, decltype(cmp)> q(cmp);

set 用 lambda

auto cmp = [](const node& a, const node& b) {
    return a.w < b.w;
};
std::set<node, decltype(cmp)> s(cmp);

map 用 lambda

auto cmp = [](const node& a, const node& b) {
    return a.w < b.w;
};
std::map<node, int, decltype(cmp)> m(cmp);

5. 总结

注意:

如需完整示例代码或有其他自定义需求,欢迎继续提问!

1. 为什么常用struct cmp而不是bool cmp?

1.1 仿函数(struct cmp)

struct cmp {
    bool operator()(const node& a, const node& b) const {
        return a.w < b.w;
    }
};

1.2 普通函数(bool cmp(const node&, const node&))

bool cmp(const node& a, const node& b) {
    return a.w < b.w;
}

2. STL容器的要求

2.1 set、map、priority_queue等

2.2 如果你想用普通函数怎么办?

3. lambda表达式(C++11及以上)

4. 总结

结论

仿函数不一定非要用struct cmp包一圈,但STL容器要求传入类型,不能直接用bool cmp函数。你可以用lambda表达式或者仿函数类型。

如需具体代码示例或有其他疑问,欢迎继续追问!

1. 成员函数重载

struct node {
    int w, pos;
    bool operator<(const node& x) const {
        return w < x.w;
    }
};

适用场景

例子

struct node {
    int w, pos;
    bool operator<(const node& x) const {
        return w < x.w;
    }
};

node a{1, 2}, b{2, 3};
if (a < b) { /* ... */ }

2. 友元函数重载

struct node {
    int w, pos;
    friend bool operator<(const node& x, const node& y) {
        return x.w < y.w;
    }
};

适用场景

例子

struct node {
    int w, pos;
    friend bool operator<(const node& x, const node& y) {
        return x.w < y.w;
    }
};

node a{1, 2}, b{2, 3};
if (a < b) { /* ... */ }

更复杂的例子:支持不同类型比较

struct node {
    int w, pos;
    friend bool operator<(const node& x, int y) {
        return x.w < y;
    }
    friend bool operator<(int y, const node& x) {
        return y < x.w;
    }
};
node a{1, 2};
if (a < 5) { /* ... */ }
if (5 < a) { /* ... */ }

3. 主要区别总结

特点 成员函数 友元/全局函数
语法 bool operator<(const node& x) const friend bool operator<(const node& x, const node& y)
调用方式 a < b 调用 a.operator<(b) a < b 调用 operator<(a, b)
左右类型 左侧必须是本类对象 左右都可以是本类对象,甚至可以不同类型
对称性 只支持 a < b 支持 a < b 和 b < a,也可支持不同类型
访问权限 只能访问公有成员 友元可访问私有成员

4. 什么时候用哪个?

5. 例子对比

成员函数

struct node {
    int w, pos;
    bool operator<(const node& x) const {
        return w < x.w;
    }
};

只能 a < b,不能 1 < a。

友元函数

struct node {
    int w, pos;
    friend bool operator<(const node& x, const node& y) {
        return x.w < y.w;
    }
    friend bool operator<(const node& x, int y) {
        return x.w < y;
    }
    friend bool operator<(int y, const node& x) {
        return y < x.w;
    }
};

可以 a < b,a < 1,1 < a。

6. 推荐

如需更详细的代码示例或有其他疑问,欢迎继续追问!