矢量下标超出范围错误的C ++ [英] vector subscript out of range error in c++

查看:535
本文介绍了矢量下标超出范围错误的C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个程序,需要n个整数的输入,并找出发生在给定的输入最多次数的人。我想运行的程序对于T案件。 对于这一点,我已实施的计数排序象算法(或许有点naiive),计数发生在输入的每个号码的数目。如果有多个号码与相同的最大事件,我需要返回在那些较小。对于这一点,我实现了排序。
我现在面临的问题是,每一个对我的Visual C ++运行程序时,我得到一个错误,告诉矢量下标超出范围。在NetBeans中,它产生的返回值1和退出。请大家帮我看看这个问题

 的#include< cstdio>
    #包括< cstdlib>
    #包括< CString的GT;
    #包括<的iostream>
    #包括<算法>
    #包括<载体>


使用名字空间std;

INT findmax(矢量< INT>一种,INT N)
{
    INT I,RET;
    保留= 0;
    对于(I = 0; I&n种;我+ +)
    {
        如果(A [1]≥保留){
                RET = A [1];
        }
    }
    返回RET;
}


诠释的main(){
    INT I = 0,J = 0,K = 0,N,M,R1,R2;
    矢量< int的>一个;
    INT吨;
    矢量< int的>浅黄色;

    CIN>>吨;
    而(T--){

        霉素>将N;
        a.clear();
        buff.clear();
        对于(I = 0; I&n种;我++){

            霉素>>在[I];
        }

        排序(a.begin(),a.end());
        M = findmax(A,N);
        为(J = 0; J&所述; m + 1个; J ++){
            BUFF [A [J] = BUFF [A [J] + 1;
        }
        K = findmax(BUFF,M + 1);

        对于(i = 0; I< M + 1;我++){
            如果(BUFF [我] == K){
                 R1 =我;
                 R2 = BUFF [I]
                 打破;
            }
        }

        COUT<< R1<<<< R2<< ENDL;
    }
    返回0;
}
 

解决方案

a.clear()矢量没有任何成员,它的大小为0

添加一个调用 a.resize(N),使其成为合适的大小。您还需要调整 BUFF 来不论大小它需要的。

I am trying to write a program that takes an input of of n integers, and finds out the one that occurs the maximum number of times in the given input. I am trying to run the program for t cases. For this, I have implemented a counting sort like algorithm (perhaps a bit naiive), that counts the number of occurrences of each number in the input. In case there are multiple numbers with the same maximum occurrence, I need to return the smaller among those. For this, I implemented sorting.
The issue I am facing is, that every time I run the program on Visual C++, I am getting an error that tells "vector subscript out of range". Under Netbeans, it is generating a return value of 1 and exiting. Please help me find the problem

   #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <vector>


using namespace std;

int findmax(vector<int> a, int n)
{
    int i,ret;
    ret = 0;
    for ( i = 0; i <n; i++)
    {
        if (a[i] > ret) {
                ret = a[i]; 
        }
    }
    return ret;
}


int main() {
    int i = 0, j = 0, k = 0, n,m,r1,r2;
    vector<int> a;
    int t;
    vector<int> buff;

    cin>>t;
    while(t--) {

        cin>>n;
        a.clear();
        buff.clear();
        for ( i = 0; i < n; i++) {

            cin>>a[i];
        }

        sort(a.begin(),a.end());
        m = findmax(a,n);
        for ( j = 0; j < m+1; j++) {
            buff[a[j]] = buff[a[j]] + 1;
        }
        k = findmax(buff,m+1);

        for ( i = 0; i < m+1; i++) {
            if (buff[i] == k) {
                 r1 = i;
                 r2 = buff[i];
                 break;
            }
        }

        cout<<r1<<" "<<r2<<endl;
    }
    return 0;
}

解决方案

After a.clear() the vector doesn't have any members, and its size is 0.

Add a call to a.resize(n) to make it the proper size. You also need to resize buff to whatever size it needs to be.

这篇关于矢量下标超出范围错误的C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆