C ++断言失败矢量在运行时表达式:矢量下标超出范围(C++ Assertion Failed on

2019-09-28 07:53发布

IM刚开这实在是烦人的错误消息。 我知道我只是在新本,但它似乎我能想出的东西类型。 谁能告诉我在哪里,我去错了吗?

在运行时的信息是: 调试断言失败! 程序:...文件:C:\ Program Files文件\微软的Visual Studio 10.0 \ VC \ \包括矢量线:932表达式:矢量下标越界

而且代码

#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)
{
}

Answer 1:

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

问题也恰恰是错误信息说:你的向量下( i )是超出范围。 具体而言,您从不增加向量的大小,所以它总是大小为0。因此,任何使用的operator[]将要参考外的范围内的元件。



Answer 2:

你不能只用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]


Answer 3:

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

从上面的评论,看来你需要大小的矢量NumberOfInts 。 但留行的评论 -

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

您在声明向量 -

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

要执行的操作[]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;
    // ....


文章来源: C++ Assertion Failed on vector at runtime Expression: vector subscript out of range