C ++ Assertion在运行时向量上失败表达式:向量下标超出范围 [英] C++ Assertion Failed on vector at runtime Expression: vector subscript out of range
问题描述
im gettin这真是讨厌的错误信息。我知道Im只是新的,但它似乎是我能找出的事情的类型。任何人都可以告诉我哪里出错了?
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?
运行时的消息是:
Debug Assertion Failed!
程序:
....
文件:c:\program files\microsoft visual studio 10.0 \vc\include\vector
Line: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)
{
}
推荐答案
没有足够的信息可以肯定地说,但我怀疑失败的线是 r>> numberVector [i]
。我想你是说 int j; r> j; numberVector.push_back(j);
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);
问题正是错误消息所说:你的向量下标( i
)超出范围。具体来说,你从不增加你的向量的大小,所以它总是大小为0.因此,任何使用 operator []
将引用一个超出范围元素。
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.
这篇关于C ++ Assertion在运行时向量上失败表达式:向量下标超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!