简体   繁体   English

调试断言失败.C ++向量下标超出范围

[英]Debug assertion failed.. C++ vector subscript out of range

The following code is supposed to remove the duplicate values in a vector. 以下代码应该删除向量中的重复值。
For example, if vector contains {1,5,3,3} the result should be {1,5,3} . 例如,如果vector包含{1,5,3,3}则结果应为{1,5,3}

The program starts and I enter the integer n* . 程序启动,我输入整数n* However, the program throws the following error: 但是,该程序会引发以下错误:

Debug assertion failed.Program : ...\\include\\vector line:932 Expression:vector subscript out of range. 调试断言失败。程序:... \\ include \\ vector line:932表达式:向量下标超出范围。

When I press retry, visual c++ displays a new window: 当我按重试时,visual c ++显示一个新窗口:

"try.exe has triggered a breakpoint". “try.exe触发了一个断点”。

Then, after I click on continue, another error appears: 然后,在我单击继续后,出现另一个错误:

Debug Assertion Failed! 调试断言失败! Program :...\\include\\vector line:933 expression:"standart c++ libraries out of range" && 0 程序:... \\ include \\ vector line:933表达式:“标准c ++库超出范围”&& 0

My code is as follows: 我的代码如下:

#include <iostream>
#include <vector>
using namespace std;
void removeDup (vector<int>& v);

int main()
{
    vector<int> v;
    int i,n;
    cin>>n;
    for(i=0;i<n;i++){
        v[i]=rand()%10;
    }
    removeDup(v);
    for(i=0;i<n;i++)
    {
        cout<<v[i];    
    }
    system("pause");
}

void removeDup(vector<int>& v)
{
    int i,j,size;
    size=v.size();
    for(i=0;i<size;i++)
    {
        for(j=0;j<size;j++)
        {
            if(v[i]==v[j])
            v.erase(v.begin()+j);
        }
    }
}

The assertion failure is actually telling you exactly what's happening. 断言失败实际上告诉你到底发生了什么。 Your vector is zero-sized, and you are trying to index an empty one (which is naturally going to access things out of bounds). 你的向量是零大小的,并且你试图索引一个空的向量(它自然会访问超出界限的东西)。

operator[] does not create elements on the fly for a standard sequence like vector as with, say, associative arrays in some languages or as with the case of std::map . operator[]不会为像vector这样的标准序列动态创建元素,比如某些语言中的关联数组,或者与std::map The index you pass to operator[] must be in the range, [0, vector.size()) or it will trigger this out of range assertion failure in debug (for checked implementations) or a potential segfault/access violation in a release build (basically undefined behavior). 传递给operator[]的索引必须在[0, vector.size())范围内,否则它将在调试中触发此超出范围的断言失败(对于已检查的实现)或在发布中触发潜在的段错误/访问冲突构建(基本上是未定义的行为)。 This is done for performance reasons as it would otherwise require branching in operator[] , and that would typically destroy the array-comparable indexing performance that vector has (though there is an at method which throws which has the branching overhead). 这是出于性能原因而完成的,否则它将需要在operator[]进行分支,这通常会破坏vector具有的与数组相当的索引性能(尽管存在一个抛出具有分支开销的at方法)。

Here you want to size the vector in advance using the fill constructor, ex: 在这里,您希望使用填充构造函数预先调整向量的大小,例如:

int i,n;
cin>>n;
vector<int> v(n);
...

Or resize it prior to accessing it with operator[] using the resize method: 或者在使用resize方法使用operator[]访问它之前调整它的resize

vector<int> v;
int i,n;
cin>>n;
v.resize(n);

Or use push_backs : 或者使用push_backs

vector<int> v;
int i,n;
cin>>n;
// can use reserve here for a performance boost if desired
// since we know the memory capacity needed in advance.
for(i=0;i<n;i++){
    v.push_back(rand()%10);
}

There is one other issue in your removeDup function. 您的removeDup函数还有另一个问题。 That one has an issue in that you are looping through the vector as though its size doesn't change, but calling an erase method which will reduce its size at every iteration. 那个问题在于你循环遍历向量,好像它的大小没有改变,但是调用一个擦除方法,它会在每次迭代时减小它的大小。 That will also invoke an out of range access -- there maybe you can figure out the solution as part of the exercise (I'm assuming this is an exercise since std::unique will do the trick). 这也会调用超出范围的访问权限 - 也许你可以找出解决方案作为练习的一部分(我假设这是一个练习,因为std::unique会做到这一点)。 First thing to note here perhaps is that operator[] doesn't create elements for you on the fly. 这里要注意的第一件事可能是operator[]不会为你动态创建元素。

I am giving you another way to solve this.(U may use it). 我给你另一种方法来解决这个问题。(你可以使用它)。 Here you can use #include<set> to remove the duplicate value like the following: 在这里,您可以使用#include<set>删除重复值,如下所示:

set<int>s;
    s.insert(10);
    int i,n;
    cin>>n;
    for(i=0;i<n;i++){
        s.insert(rand()%10);
    }
    set<int>::iterator ii;
    for(ii=s.begin();ii!=s.end();ii++)
    cout<<*ii;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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