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}
.
The program starts and I enter the integer n*
.
However, the program throws the following error:
Debug assertion failed.Program : ...\include\vector line:932 Expression:vector subscript out of range.
When I press retry, visual c++ displays a new window:
"try.exe has triggered a breakpoint".
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
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);
}
}
}
v.push_back(rand()%10);
orv.resize(n);
before starting the loop. – πάντα ῥεῖ