重载运算符 = 中断 std::sort
Overloading operator= breaks std::sort
可能是骗子,但我找不到它。
在双节棍两天后,我发现重载等于运算符(operator=
(显然破坏了std::sort
。 也许我不正确地超载operator=
? 这是我的MCVE:
#include <algorithm>
#include <functional>
#include <iostream>
#include <string>
#include <cstdint>
#include <vector>
struct Person
{
std::string name;
uint32_t age;
bool operator< (const Person& p)
{
return this->age < p.age;
}
Person operator= (const Person& p)
{
Person newP;
newP.name = p.name;
newP.age = p.age;
return newP;
}
static bool SortPeople(const Person& p1, const Person& p2)
{
return p1.age < p2.age;
}
};
void PrintPeople(const std::vector<Person>& people)
{
std::cout << "============ people begin" << std::endl;
for (const auto& pIt : people)
{
std::cout << "name: " << pIt.name << ", age: " << pIt.age << std::endl;
}
std::cout << "============ people end" << std::endl;
}
int main()
{
std::vector<Person> people = { { "james", 12 },
{ "jada", 4 },
{ "max", 44 },
{ "bart", 7 }
};
PrintPeople(people);
std::sort(people.begin(), people.end());
PrintPeople(people);
return 0;
}
如果我按原样运行此代码,则不会根据年龄对每个人进行排序。 PrintPeople
按照我初始化people
的相同顺序打印出来。 但是,如果我注释掉整个Person operator=
函数,那么people
确实会根据年龄按升序打印出来。 无论我调用 std::sort(people.begin(), people.end());
还是 std::sort(people.begin(), people.end(), Person::SortPeople);
,我都会看到相同的行为,无论我使用 7.2.1 版还是 4.0.1 版g++
clang++
我都会看到相同的行为。 我正在运行 Fedora 27。
任何人都知道为什么过载operator=
会破坏std::sort
?
我正在编译标志-Wall -Wextra -Wconversion -std=c++11
,没有警告。
是的,你错误地重载了operator=
。它应该在*this
上修改,但您正在修改本地对象newP
。
将其更改为
Person& operator= (const Person& p)
{
name = p.name;
age = p.age;
return *this;
}
std::sort
通过移动元素对元素进行排序,这使用重载的operator=
进行Person
。这就是为什么错误的实现在这里中断std::sort
的原因。
顺便说一句:当您删除实现时,自动生成的赋值运算符会为您做正确的事情。
>你的operator=
重载从根本上被破坏了:它返回一个新对象(而不是像强约定那样对*this
的引用(,而完全不影响*this
。
- 为什么 std::unique 不调用 std::sort?
- std::sort()函数无法对向量的一部分进行排序
- 使用自定义比较函数使用std::sort()对矢量字符串进行排序时出现问题
- C++中"std::sort"比较器的不同类型
- 如何使 std::sort 在 std::swap 和我的命名空间的模板化交换之间没有名称冲突?
- 将用户定义的类型与 std::vector 和 std::sort 一起使用
- 使用 std::sort 对向量进行稳定排序
- std::sort 如何处理重复的数字?
- 为什么 std::sort 找不到合适的(静态成员)函数重载?
- 尝试使用谓词函数会导致错误:"std::sort"未找到匹配的重载函数
- unqualified sort() -- 为什么它在 std::vector 上使用而不是在 std::array 上
- 使用 std::sort 对二维 c 数组进行排序
- std::stable_sort vs std::sort
- std::sort 导致运算符分段错误<
- 使用 std::sort 对 C 样式的 2D 数组进行部分排序
- std::vector 未按 std::sort 的预期排序
- 隐式转换为比较函数对象(函子)用于 std::sort 而不是 std::map?
- 为什么只有当我的容器有超过 32 个元素时才由 std::sort 调用交换?
- 在 c++ 中使用 std::sort 与迭代器和模板
- 使用类似的比较函数时,在 c++ 中为 std:sort 获得不同的结果