Vector subscript out of range error
Mar 30, 2014 at 2:27am UTC
I´m suppose to create a function that implements bubblesort my problem is I keep getting the same error over and over again "Vector subscript out of range error" and I don´t know how to stop it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
void bubblesort(vector<int > &v)
{
int limit = v.size() - 1;
while (limit > 0)
{
for (int k = 0; k < limit; k++)
{
if (v[k]>v[k + 1])
{
vector<int > subst;
subst[0] = v[k + 1];
v[k + 1] = v[k];
v[k] = subst[0];
}
else
{
continue ;
}
}
limit--;
}
}
Mar 30, 2014 at 2:48am UTC
1 2
vector<int > subst; //an empty vector
subst[0] = v[k + 1]; //trying to access its first element ¿?
Mar 30, 2014 at 9:38am UTC
Thank you,now I that I changed to
1 2 3
vector<int > subst; //an empty vector
subst.push_back = v[k + 1];
It works just fine,thank you!
Topic archived. No new replies allowed.