Vector of struct erase

Helo!

I'm working on a program and I came across with a weird problem. I created a struct with a string and an int. Then I made a vector with the type struct.

1
2
3
4
5
6
7
struct X{
     string a;
     int b;
}

vector<X> v;
//add some values 


I added some values and I did what I want. But when I try to erase the last n element, I get runtime error. How can I do properly?

Thank you very much!
Last edited on
closed account (2LzbRXSz)
Do you mean erase as in vector.erase(/*parameters*/);, or actually deleting lines from your code? What error are you getting?
Last edited on
Yes, I erase like that:
 
v.erase(v.end()-n,v.end());


Yesterday it didn't work, I don't know what was the problem. But now it working!

Thanks! :)
closed account (2LzbRXSz)
No problem, glad to hear it's working. Looks like you *Won*

hehe
Yees :D
Note that it only works if v has at least n elements.
Yes I know, the n is calculated from the number of elements, so it's always less then the size of vector.
But thanks for advice.
I just thought I mention it in case it was what caused the problem in the first case.
Topic archived. No new replies allowed.