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

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() 自帶的來完成.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • threenewbee 2017-05-03 15:48
    关注
    评论
  • crazyboy12138 2017-05-04 02:03
    关注

    list l;

    (list & l)

    list::iterator ...

    
    
    评论
  • crazyboy12138 2017-05-04 02:04
    关注

    list。。。。。。。。。。。

    评论
  • crazyboy12138 2017-05-04 02:04
    关注

    “list”

    评论
  • crazyboy12138 2017-05-04 02:06
    关注

    有毒啊,< in t>显示不出来,,,得在int两边加空格

    评论
  • crazyboy12138 2017-05-04 02:13
    关注

    看来是list不支持这样,直接l.sort(),或者l.sort( compare)就行

    评论
  • jalen_zh 博客专家认证 2017-05-05 08:26
    关注

    sort好像只能对连续空间存储的数据排序,例如数组,vector

    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥15 k210显示failed init to model
  • ¥15 Evil-droid生成的APK手机已经下载但无法建立任务
  • ¥25 c语言韩信点兵的变式
  • ¥15 怎么根据书上的例子完成这个问题呢?
  • ¥15 ECharts 增加Zoom,整行包括右边的Text一起滑动
  • ¥15 关于网上一个easyx制作的见缝插针小游戏(c++)
  • ¥15 开地址法双散列函数处理碰撞
  • ¥15 想问一下这个是什么情况 虚拟机Linux打不开了
  • ¥15 联通光猫掉注册了怎么重新注册上去
  • ¥15 关于unity开发steamvr程序遇到的问题