码迷,mamicode.com
首页 > 其他好文 > 详细

stl sort使用不当造成崩溃

时间:2019-01-14 20:08:26      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:clu   bool   iostream   space   sort   tor   char   ios   bsp   


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

bool compare(int a, int b)
{
return a >= b;
}

int main(int argc, char *argv[])
{
vector<int> vec;

for (int i = 0; i < 17; i++)
{
int x = 0;
vec.push_back(x);
}

sort(vec.begin(), vec.end(), compare);

return 0;
}
      compare必须针对相等返回false,  显然, 上面程序中, 当a和b相等时, compare返回了true,  这是有问题的

stl sort使用不当造成崩溃

标签:clu   bool   iostream   space   sort   tor   char   ios   bsp   

原文地址:https://www.cnblogs.com/wangshaowei/p/10268488.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!