对于初学者来说,标准容器std::list 具有接受比较函数对象的成员函数merge。他们是
template <class Compare>
void merge(list& x, Compare comp);
template <class Compare>
void merge(list&& x, Compare comp);
所以你可以使用它们。
因此,您可以选择使用标准算法 std::merge 或使用 std::list 的这些成员函数。
考虑到如果您希望结果列表也根据此函数排序,则应使用用于对列表进行排序的相同比较函数sort_func()。
这是一个演示程序,展示了如何仅使用类std::list 的本机方法来完成任务。而不是你的比较函数,而是在标题<functional>中声明的函数对象std::greater
#include <iostream>
#include <list>
#include <functional>
int main()
{
std::list<int> list1 = { 3, 1, 5, 9, 7 };
std::list<int> list2 = { 8, 0, 4, 6, 2 };
list1.sort( std::greater<int>() );
list2.sort( std::greater<int>() );
std::list<int> list( list1 );
list.merge( list2, std::greater<int>() );
for ( int x : list ) std::cout << x << ' ';
std::cout << std::endl;
return 0;
}
程序输出是
9 8 7 6 5 4 3 2 1 0