472,121 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,121 software developers and data experts.

delete vector element

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..

any idea?

Dec 16 '06 #1
5 11846
st*******@gmail.com wrote:
void table::delel( int index )
{
vector< record >::iterator rm = db.begin();
for( int i = 0; i < index; i++ )
rm++;
Vector iterators are random access, you could do:
db.erase(db.begin() + index();
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..
That's to be expected. The actual object in the vector
belong to the vector. Deleting one typically involves
copying (by assignment) all the later ones to one position
earlier and then calling the destructor on the last.

The things you store in the vector must be copy contructable
and assignable, so that this sort of strategy works.
Dec 16 '06 #2
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 );
}
void table::delel(std::vector<record>::difference_type index)
{
db.erase(db.begin() + index);
}

Best regards,

Tom

Dec 16 '06 #3

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

Dec 16 '06 #4

Ï/Ç 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 :)

Dec 16 '06 #5

StreamKid wrote:
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 :)
Ohhh, I see, than Tom's solution looks good.

--
Ivan
http://www.0x4849.net

Dec 17 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Maximus | last post: by
11 posts views Thread by Peter Olcott | last post: by
11 posts views Thread by DamonChong | last post: by
9 posts views Thread by david wolf | last post: by
35 posts views Thread by Jon Slaughter | last post: by
7 posts views Thread by JH Programmer | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.