我有以下情况:
#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/