C ++向量下标超出了1201行

时间:2015-04-08 08:56:21

标签: c++ vector range max subscript

我正在开发一个程序,该程序从用户那里获得成绩并返回最大,最小,平均,中位数和标准偏差。每当我尝试运行我的代码时,我都会遇到此错误:

文件:c:\ program files(x86)\ microsoft visual studio 12.0 \ vc \ include \ vector

行:1201

表达式:矢量下标超出范围

有人能告诉我我做错了什么吗?不管我做什么,我似乎无法修复它。完全是C ++编码的新手,所以如果你能深入解释那将是非常有帮助的。

这是它在向量文件中引用的代码块:

#if _ITERATOR_DEBUG_LEVEL == 2
if (size() <= _Pos)
{   // report error
    _DEBUG_ERROR("vector subscript out of range");
    _SCL_SECURE_OUT_OF_RANGE;
}

具体指向这一行:

    _DEBUG_ERROR("vector subscript out of range");

我的代码:

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

void enterGrades()
{
    int count = 0;//initialize count
    int grade;//initialize grade variable
    int maxGrade = 0;
    vector<int> gradeList;//initialize vector
    vector<int>::iterator gradeListIt;//initialize iterator
    do {
        cout << "Enter grades [0 to exit]: ";//enter grades
        cin >> grade;//user input
        count += 1;//for each grade, increase count
        gradeList.push_back(grade);//append grades to vector    
    } while (grade != 0);
};

int maxNum(vector<int> gradeList)
{
    int largest = gradeList[0];//initialize largest number
    int length = gradeList.size();
    gradeList.resize(length);
    for (int value = 0; value < length; value++)//traverse vector
    {
        if (gradeList[value] > largest)//if current value > largest
            largest = gradeList[value];//set largest to that value
    }
    return largest;//return largest

};

int minNum(vector<int> gradeList)
{
    int smallest = gradeList[0];
    int length = gradeList.size();
    gradeList.resize(length);
    for (int value = 0; value < length; value++)
    {
        if (gradeList[value] < smallest)
            smallest = gradeList[value];
    }
    return smallest;
};

int avgNum(vector<int> gradeList)
{
    int total = 0;
    int length = gradeList.size();
    gradeList.resize(length);
    for (int value = 0; value < length; value++)
    {
        total += value;
    }
    return total / length;

};

//int stdDev (vector<int>& gradeList)
//{
//    int variance = 0;
//    int avg = avgNum(vector<int>& gradeList);
//    int length = gradeList.size();
//    for(int value = 1; value < length; value++)
//    {
//            variance = variance + pow(value - avg, 2);
//    }
//    variance = pow(variance / length, 0.5);
//    return variance;
//    
//};



int main()
{
    vector<int> gradeList;//initialize vector
    vector<int>::iterator gradeListIt;//initialize iterator
    enterGrades();//
    cout << "Maximum grade is: " << maxNum(gradeList) << endl;
    cout << "Minimum grade is: " << minNum(gradeList) << endl;
    cout << "Average grade is: " << avgNum(gradeList) << endl;

}

另外,在我的主要功能中;我可以像输出语句中那样调用其他函数吗?

1 个答案:

答案 0 :(得分:1)

赋予同名的东西并不能使它们成为同一个东西。

enterGrades返回后,名为&#34; gradeList&#34;的向量在main中仍然是空的 当您尝试访问空向量的第一个元素时,会出现错误。

您的main应如下所示:

int main()
{
    std::vector<int> gradeList = enterGrades();
    cout << "Maximum grade is: " << maxNum(gradeList) << endl;
    cout << "Minimum grade is: " << minNum(gradeList) << endl;
    cout << "Average grade is: " << avgNum(gradeList) << endl;
}

enterGrades的实施更改为适合作为练习。

此外,你还在做这件事:

int length = gradeList.size();
gradeList.resize(length);

没有意义,因为它会将矢量调整到已有的尺寸。
删除它。

您也无缘无故地重复声明迭代器。

您还可能想要查看您应该计算的平均值,因为您正在计算向量中的平均索引,而不是平均值。
并注意那些整数除法。