c++ - std::list::merge() 对包含对象的列表失败

标签 c++ c++11

我有以下情况:

#include <list>

struct Example
{
    double p1;
    double p2;
};

void f()
{
    std::list<Example> list1;
    std::list<Example> list2;
    list1.merge(list2);
}

在构建过程中出现错误:

C2672 'operator __surrogate_func': no matching overloaded function found

C2893 Failed to specialize function template 'unknown-type std::less<void>::operator() (_Ty1 &&,_Ty2&&) const'

如果我注释最后一行代码,则构建成功。我发现很难相信包含对象的列表不能合并,所以:我错过了什么?

附言。我使用 Visual Studio Community 2015

最佳答案

documentation 中所述std::list::merge :

Merges two sorted lists into one.

还有:

The first version uses operator< to compare the elements

所以你要么需要提供operator<为您的结构或使用带有自定义比较器的重载版本。例如独立函数可以是:

bool operator<( const Example &e1, const Example &e2 ) {
     return std::tie( e1.p1, e1.p2 ) < std::tie( e2.p1, e2.p2 );
}

关于c++ - std::list::merge() 对包含对象的列表失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43213589/

相关文章:

c++ - 字符串比较运算符的含义是什么?

c++11 - 继承和 OpenCL 的问题

c++ - 使用 std::move 和 std::shared_ptr 有什么好处和风险(如果有的话)

c++ - 有人可以解释这些函数返回类型的区别吗

c++ - 为什么当枚举或 int 值作为函数的 bool 参数传递时 gcc 不发出警告?

c++ - Qt 样式表影响整个小部件

c++ - std::to_string(double) 的精度

c++ - 获取没有实例的 std::bitset 的大小

c++ - 将编译器更改为Clang

c++ - 在 C++ 中查找未知对象的类型