【发布时间】:2017-11-04 17:25:48
【问题描述】:
我在this answer 中为我的类重载了交换函数,但在排序时 (std::sort) 编译器仍在使用std::swap。我认为我的方法与链接答案中所述的方法没有任何区别。这是我的代码的复制:
#include <vector>
#include <iterator>
#include <algorithm>
#include <iostream>
struct B
{
struct A
{
friend void swap(A & a, A & b)
{
std::swap(a.a, b.a);
std::cout << "my swap\n";
}
A(int _a) : a(_a) {}
bool operator<(const A & other) { return a < other.a; }
int a;
};
};
int main()
{
std::vector<B::A> v{1, 2, 3, 5, 4};
std::sort(std::begin(v), std::end(v));
}
还提供了可执行示例here。
【问题讨论】: