C++运行时向量断言失败表达式:向量下标超出范围
我收到了这个非常烦人的错误消息。我知道我对此很陌生,但这似乎是我能弄清楚的类型。谁能告诉我哪里出了问题吗?
运行时的消息是: 调试断言失败! 程序: .... 文件:c:\program files\microsoft Visual Studio 10.0\vc\include\vector 线路: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)
{
}
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)
{
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我们没有足够的信息来确定,但我怀疑失败的行是
r >>>; numberVector[i]
。我想你的意思是int j; r>> j; numberVector.push_back(j);
问题正是错误消息所说的:您的向量下标 (
i
) 超出范围。具体来说,您永远不会增加向量的大小,因此它的大小始终为 0。因此,operator[]
的任何使用都将引用超出范围的元素。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 sayint 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 ofoperator[]
is going to reference an out-of-range element.您不能只使用
numberVector[i]
而不先调用numberVector.resize()
。You can't just use
numberVector[i]
without callingnumberVector.resize()
first.从上面的评论来看,您似乎需要一个大小为
NumberOfInts
的向量。但保留注释行 -您将向量声明为 -
要在
numberVector
上执行[]
操作,应提及其大小并且应在有效范围内同时声明。由于在声明时没有提到,因此您需要执行push_back
操作来动态增加向量的大小。From the above comment, it seems you need a vector of size
NumberOfInts
. But leaving the line as commented -You are declaring the vector as -
To perform the operation of
[]
onnumberVector
, 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 apush_back
operation to dynamically increase the size of the vector.