维1 2017-05-03 15:13 采纳率: 66.7%
浏览 5601
已采纳

c++ stl 中sort对list容器的排序

#include
#include
#include

using namespace std;

void PrintList(list &l)
{
for (list::iterator corrent = l.begin(); corrent!=l.end(); corrent++)
{
/*int tmp = l.front();
cout << tmp << endl;*/
;
cout << *corrent << endl;
}
}

int compare(int &a,int &b)
{
return a < b;
}

int main()
{
list l;
for (int j = 0; j < 10; j++)
{
l.push_back(j);
//l.push_front(j);
}
sort(l.begin(), l.end());//这两个为什么都是错的???
sort(l.begin(), l.end(), compare);

getchar();
return 0;
}

/*错误    1   error C2784: “unknown-type std::operator -(std::move_iterator<_RanIt> &,const std::move_iterator<_RanIt2> &)”: 未能从“std::_List_iterator<std::_List_val<std::_List_simple_types<int>>>”为“std::move_iterator<_RanIt> &”推导 //模板 参数  f:\学习软件\vs2013\vc\include\algorithm 3157    1   List入门

*/

  • 写回答

8条回答 默认 最新

  • jsnhsu1971 2017-05-03 16:01
    关注

    std:sort() 所使用的容器必須能支持隨機存取, 如: std::vector

    你使用的 std::list 非隨機存取的容器, std::list 排序可使用 std::list::sort() 自帶的來完成.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵