简体   繁体   中英

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

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

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

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); 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.

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

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]
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 -

//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_back operation to dynamically increase the size of the vector.

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;
    // ....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM