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

发布于 2024-11-02 07:32:12 字数 2739 浏览 2 评论 0原文

我收到了这个非常烦人的错误消息。我知道我对此很陌生,但这似乎是我能弄清楚的类型。谁能告诉我哪里出了问题吗?

运行时的消息是: 调试断言失败! 程序: .... 文件: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

肤浅与狂妄 2024-11-09 07:32:12

我们没有足够的信息来确定,但我怀疑失败的行是 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 say 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.

樱桃奶球 2024-11-09 07:32:12

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

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]
月亮坠入山谷 2024-11-09 07:32:12
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;
    // ....
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;
    // ....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文