C++ 断言在运行时在向量上失败 表达式:向量下标超出范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5736813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:46:02  来源:igfitidea点击:

C++ Assertion Failed on vector at runtime Expression: vector subscript out of range

c++vectorassertion

提问by Tanya Tazzy Hegarty

im gettin this really annoying error message. I know Im only new to this but it seems the type of thing I could figure out. Can anyone show me where im going wrong please?

我开始看到这个非常烦人的错误信息。我知道我对此只是新手,但这似乎是我能弄清楚的事情。谁能告诉我我哪里出错了?

The message at run time is: Debug Assertion Failed! Program: .... File: c:\program files\microsoft visual studio 10.0\vc\include\vector Line: 932 Expression: Vector subscript out of range

运行时的消息是: 调试断言失败!程序:.... 文件:c:\program files\microsoft visual studio 10.0\vc\include\vector 行:932 表达式:向量下标超出范围

and the code is

代码是

#include "VectorIntStorage.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

void VectorIntStorage::Read(istream& r)
{
    char c[13];
    r >> c;
    r >> NumberOfInts; //gets number of ints for vector

    //numberVector = new std::vector<int> numberVector;

    for(int i = 0; i < NumberOfInts; i++)
    {
        r >> numberVector[i];
        cout << numberVector[i] << endl;

        if(_sortRead) //true
        {
            for(int k = 0; k < i; k++)
            {
                if(numberVector[i] < numberVector[k])
                {
                    int temp = numberVector[k];
                    numberVector[k] = numberVector[i];
                    numberVector[i] = temp;
                }
            }
        }
    }
}

void VectorIntStorage::Write(ostream& w)
{
    for(int i = 0; i < NumberOfInts; i++)
    {
        w << numberVector[i] << endl;
        cout << numberVector[i] << endl;
    }
}

void VectorIntStorage::sortStd()
{
    sort(numberVector.begin(), numberVector.end());
}

void VectorIntStorage::sortOwn()
{
    quickSort(0, NumberOfInts - 1);
}

void VectorIntStorage::setReadSort(bool sort)
{
    _sortRead = sort;
}

void VectorIntStorage::quickSort(int left, int right)
{
     int i = left, j = right;
      int tmp;
      int pivot = numberVector[(left + right) / 2];

      while (i <= j)
      {
            while (numberVector[i] < pivot)
                  i++;
            while (numberVector[j] > pivot)
                  j--;
            if (i <= j) 
            {
                  tmp = numberVector[i];
                  numberVector[i] = numberVector[j];
                  numberVector[j] = tmp;
                  i++;
                  j--;
            }
      }

      if (left < j)
      {
            quickSort(left, j);
      }
      if (i < right)
      {
            quickSort(i, right);
      }
}

VectorIntStorage::VectorIntStorage(const VectorIntStorage& copying)
{
    //int *duplicate = new int[(copying.NumberOfInts)];
    //vector<int> *duplicate = new vector<int>;

    //std::copy(numberVector.begin(), numberVector.end(), duplicate);
    //numberVector = duplicate;
    //NumberOfInts = copying.NumberOfInts;
}

VectorIntStorage::VectorIntStorage(void)
{
}


VectorIntStorage::~VectorIntStorage(void)
{
}

回答by Rob?

We don't have enough information to say for sure, but I suspect the failing line is r >> numberVector[i]. I suppose you meant to say int j; r >> j; numberVector.push_back(j);

我们没有足够的信息可以肯定地说,但我怀疑失败的线路是r >> numberVector[i]. 我想你是想说int j; r >> j; numberVector.push_back(j);

The problem is precisely what the error message says: your vector subscript (i) is out of range. Specifically, you never increase the size of your vector, so it is always of size 0. Thus, any use of operator[]is going to reference an out-of-range element.

问题正是错误消息所说的:您的向量下标 ( i) 超出范围。具体来说,您永远不会增加向量的大小,因此它的大小始终为 0。因此,任何使用operator[]都将引用超出范围的元素。

回答by Erik

You can't just use numberVector[i]without calling numberVector.resize()first.

你不能不先numberVector[i]打电话就使用numberVector.resize()

vector<int> vec;
vec[1] = 0; // fails - vec is empty so [1] is out of range
vec.resize(100);
vec[1] = 5; // ok, you can access vec[0] .. vec[99] now
vec.push_back(11); // Now the size is 101 elements, you can access vec[0] .. vec[100]

回答by Mahesh

r >> NumberOfInts; //gets number of ints for vector

From the above comment, it seems you need a vector of size NumberOfInts. But leaving the line as commented -

从上面的评论来看,您似乎需要一个 size 的向量NumberOfInts。但留下评论的那一行——

//numberVector = new std::vector<int> numberVector;

You are declaring the vector as -

您将向量声明为 -

std::vector<int> numberVector; // The size of the vector is 0

To perform the operation of []on numberVector, it's size should be mentioned and should be in the valid range while declaration. Since it not mentioned while declaration, you need to do a push_backoperation to dynamically increase the size of the vector.

要执行[]on的操作,numberVector应该在声明时提到它的大小并且应该在有效范围内。因为在声明时没有提到,所以需要做一个push_back操作来动态增加向量的大小。

for(int i = 0; i < NumberOfInts; i++)
{
    r >> numberVector[i];    // Size isnot initially mentioned while declaration 
                             // of the vector to do an `[]` operation
    cout << numberVector[i] << endl;
    // ....