naw*_*bgh 7 c++ operator-overloading
为什么std :: sort operator<在这段代码中没有使用我的实现
#include <iostream>
#include <vector>
#include <tuple>
#include <algorithm>
using namespace std;
bool operator<(
const tuple<int, int>& t1,
const tuple<int, int>& t2
) {
return get<1>(t1) > get<1>(t2);// `>` so that it gets sorted in reverse
}
int main() {
vector<tuple<int, int>> v;
for (int i = 0; i < 10; ++i) {
v.push_back(make_tuple(0, i));
}
cout << "before sort: ";
for (auto& x : v) { cout << get<1>(x) << ", "; }
cout << endl;
auto v2 = v;
sort(v2.begin(), v2.end());
cout << "after sort(begin, end): ";
for (auto& x : v2) { cout << get<1>(x) << ", "; }
cout << endl;
sort(v.begin(), v.end(), [](auto& t1, auto& t2) {
return get<1>(t1) > get<1>(t2);// `>` so that it gets sorted in reverse
});
cout << "after sort(begin, end, comparator): ";
for (auto& x : v) { cout << get<1>(x) << ", "; }
cout << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
before sort: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
after sort(begin, end): 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
after sort(begin, end, comparator): 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
Run Code Online (Sandbox Code Playgroud)
我期望的输出是:
before sort: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
after sort(begin, end): 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
after sort(begin, end, comparator): 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
Run Code Online (Sandbox Code Playgroud)
这与名称查找在函数模板中的工作方式有关(通常称为两阶段查找).std::sort定义在<algorithm>,并且查找<将在模板定义(在您的模板定义不是)的范围内找到这些名称,并在相关的函数参数的关联命名空间中找到这些名称(这std::tuple将是namespace std,它们也不包括你的).
由于所讨论的参数位于命名空间中std,因此将重载添加到该命名空间中实际上是未定义的行为.因此,您可以选择坚持默认行为(这将是词典编纂<)或提供您自己的自定义比较器(就像您在问题中所做的那样).