加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

分类功能C分段故障

发布时间:2020-12-16 05:31:30 所属栏目:百科 来源:网络整理
导读:在这段代码中,对于向量大小,n = 32767,它给出了分段错误,但是直到32766,它运行正常.什么可能是错误?这是完整的代码. #includecstdio#includecstring#includecmath#includequeue#includeutility#includealgorithm#includesys/time.husing namespace std;#def
在这段代码中,对于向量大小,n> = 32767,它给出了分段错误,但是直到32766,它运行正常.什么可能是错误?这是完整的代码.
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<utility>
#include<algorithm>
#include<sys/time.h>
using namespace std;
#define MAX 100000

bool compare(pair<int,int> p1,pair<int,int> p2) {
    if(p1.second < p2.second)
        return 1;
    else if(p1.second > p2.second)
        return 0;
    if(p1.first <= p2.first)
        return 1;
    else
        return 0;
}

int main() {
    freopen("randomin.txt","r",stdin);
    int n;
    scanf("%d",&n);
    vector< pair<int,int> > p(n);
    for(int i=0;i<n;i++)
        scanf("%d%d",&p[i].first,&p[i].second);
    **printf("%dn",(int)p.max_size()); // prints 536870911**
    sort(p.begin(),p.begin()+n,compare);

    //for(int i=0;i<n;i++)
        //printf("%d %dn",p[i].first,p[i].second);
        printf("%.6fn",(p[n-1].second+p[n-2].second)/(20.0+p[n-1].first+p[n-2].first));

    return 0;
}

解决方法

这可能与您的细分错误无关,但…

在C中,你的“比较”谓词必须是strict weak ordering.特别是“比较(X,X)”必须为任何X返回“false”.在比较函数中,如果两个对都相同,则打到测试(p1.first< = p2.first),并返回“true”.因此,这个“比较”谓词没有强制严格的弱排序,而将其传递给“排序”的结果是未定义的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读