Ï/Ç Ivan Novick Ýãñáøå:
st*******@gmail.com wrote:
i have a class table, which has a vector of records(-db). i 'm trying
to remove an element,
but it doesn't seem to work..
i read this [http://www.cppreference.com/cppvector/erase.html] and
that's the function i 've written:
void table::delel( int index )
{
vector< record >::iterator rm = db.begin();
for( int i = 0; i < index; i++ )
rm++;
db.erase( rm );
}
after i execute this function, the record isn't deleted, insted it
takes all the values from the
next one( if there is any ). else.. it crashes..
What are you doing? The code you have written appears to increment an
iterator that starts at the beginning the same number of times as
elements you have. So if you have 3 elements you will start at the
beginning and increment the iterartor 3 times which leaves your
iterator pointing to the end() which is one after the last element in
your list. Then you try to erase that item, which is not an item in
your vector. Please tell us which item do you actually want to erase?
---
Ivan
http://www.0x4849.net
records are displayed, and user inputs what record he want to delete. (
this happens in other functions ).. the number of the record user
inputs is index, and is a parameter for the delete function.. the
iterator is incremented, because it starts from the beggining of the
vector, till it reaches the wanted point.( user will never want to
delete the first element, and the case that the vector is empty is
checked in another function ). then, what the iterator points to has to
be removed. that's all :)