在做pat乙级1082 射击比赛时 遇到了sort 段错误。

题目链接:https://www.patest.cn/contests/pat-b-practise/1082

感觉写的没啥毛病 但就是段错误 ,搜索了一下才明白,原来是sort函数在极端相等的情况下,比如比较的元素内容完全相同,sort会一直遍历下去直到越界。



我原来的写法是:

int compar(stu s1,stu s2){
return s1.juli-s2.juli;

}

记得以前这样写是没问题,还是我记错了,在java中写没问题?

反正以后记住比较器用bool返回值就好了额。。!!

#include<iostream>

#include<stdio.h>
#include<algorithm>
using namespace std;
#include<vector>
#include<string>
#include<cmath>

struct stu{
string bianhao;
int x;
int y;
int juli;

};
bool compar(stu s1,stu s2){
return s1.juli>s2.juli;

}
int main(){
int k;
scanf("%d",&k);
vector<stu> vec;
for(int i=0;i<k;i++){
stu s ;
cin>>s.bianhao>>s.x>>s.y;
s.juli = s.x*s.x+s.y*s.y;
vec.push_back(s);

}
if(k==0){

return 0;
}
sort(vec.begin(),vec.end(),compar);

cout<<vec[k-1].bianhao<<" "<<vec[0].bianhao;
return 0;
}

c++ 的vector sort遇到栈错误的更多相关文章

  1. [转] C++的STL库,vector sort排序时间复杂度 及常见容器比较

    http://www.169it.com/article/3215620760.html http://www.cnblogs.com/sharpfeng/archive/2012/09/18/269 ...

  2. STL vector+sort排序和multiset/multimap排序比较

    由 www.169it.com 搜集整理 在C++的STL库中,要实现排序可以通过将所有元素保存到vector中,然后通过sort算法来排序,也可以通过multimap实现在插入元素的时候进行排序.在 ...

  3. 发现一个c++ vector sort的bug

    在开发中遇到一个非常诡异的问题:我用vector存储了一组数据,然后调用sort方法,利用自定义的排序函数进行排序,但是一直都会段错误,在排序函数中打印参加排序的值,发现有空值,而且每次都跟同一个数据 ...

  4. vs2010下sort比较函数链接错误问题

    环境:win7 + vs2010 + C++ 实现vector的sort算法,在类的头文件中写入比较函数时会出现链接错误: error LNK2005: "bool __cdecl comp ...

  5. [CareerCup] 3.6 Sort Stack 栈排序

    3.6 Write a program to sort a stack in ascending order (with biggest items on top). You may use at m ...

  6. 【转】 std list/vector sort 排序

    [转自]http://blog.csdn.net/marising/article/details/4567531 网上江湖郎中和蒙古大夫很多,因此,此类帖子也很多.关于排序,我还真没研究过,看了江湖 ...

  7. vector sort AND 友元

    # include<iostream> # include<string> # include<algorithm> # include<stdio.h> ...

  8. [LeetCode] 56. Merge Intervals(vector sort)

    /** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0 ...

  9. std list/vector sort 自定义类的排序就是这么简单

    所以,自己研究了一下,如下:三种方式都可以,如重写<,()和写比较函数compare_index.但是要注意对象和对象指针的排序区别. 1.容器中是对象时,用操作符<或者比较函数,比较函数 ...

随机推荐

  1. [python每日一练]--0012:敏感词过滤 type2

    题目链接:https://github.com/Show-Me-the-Code/show-me-the-code代码github链接:https://github.com/wjsaya/python ...

  2. iPhone8的十面埋伏

    ​ 不知不觉,iPhone已经走到了第十个年头,也正因如此,业界最普遍的预测就是:iPhone8会出现颠覆性创新,让人眼前一亮的同时,给苹果再度续命.平心而论,苹果早就青史留名,创造了大量的奇迹,科技 ...

  3. Linux统计目录下文件个数及代码行数

    1. 统计当前目录下,php文件数量 find ./ -name "*.php" | wc -l 2. 统计当前目录下所有php文件代码行数 find ./ -name " ...

  4. C++走向远洋——46(教师兼干部类、多重继承、派生)

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  5. 题解-[HNOI2001]遥控赛车比赛

    题解-[HNOI2001]遥控赛车比赛 前置知识:记忆化搜索.\(\texttt{Bfs}\). 参考资料 https://www.luogu.com.cn/blog/CYJian/solution- ...

  6. JZOJ 1774. 合并果子 (Standard IO)

    1774. 合并果子 (Standard IO) Time Limits: 1000 ms Memory Limits: 65536 KB Description 在一个果园里,多多已经将所有的果子打 ...

  7. Javascript中的Math.max()和Math.min()

    Math.max()是求最大值,Math.min()是求最小值 Math.max(value1,value2,value3....) 但是如果是数组或者对象呢? var numArr = [1,2,4 ...

  8. iOS 17个常用代码整理

    .判断邮箱格式是否正确的代码: //利用正则表达式验证 -(BOOL)isValidateEmail:(NSString *)email { NSString *emailRegex = @" ...

  9. Redis系列三 - 缓存雪崩、击穿、穿透

    前言 从学校出来,做开发工作也有一定时间了,最近有想系统地进一步深入学习,但发现基础知识不够扎实,故此来回顾基础知识,进一步巩固.加深印象. 最初开始接触编程时,总是自己跌跌撞撞.不断摸索地去学习,再 ...

  10. django 博客搭建

    comment1.安装django pip install django 2.创建项目 django-admin startproject mysite 3.在mysite文件夹下创建app pyth ...