By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,950 Members | 1,032 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,950 IT Pros & Developers. It's quick & easy.

Deleting elements out of a vector

P: n/a
Hi.

Is there a possibility to delete elements out of a vector?
At the moment i do it by copying all needed elements to another vector.
After clearing the old vector i copy them back element by element.

Regards!
Christian
Sep 17 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Christian Bruckhoff schrieb:
Hi.

Is there a possibility to delete elements out of a vector?
At the moment i do it by copying all needed elements to another vector.
After clearing the old vector i copy them back element by element.
Look up erase, for instance at
http://www.cppreference.com/cppvector/erase.html

-- Markus
Regards!
Christian

Sep 17 '06 #2

P: n/a
In article <ee**********@cache.uni-koblenz.de>,
"Christian Bruckhoff" <br******@uni-koblenz.dewrote:
Hi.

Is there a possibility to delete elements out of a vector?
At the moment i do it by copying all needed elements to another vector.
After clearing the old vector i copy them back element by element.
All sequences, including std::vector have an 'erase' member-function.

a.erase(p)

p is a dereferenceable iterator in a.

Destroys the element pointed to by p and removes it from a.

a.size() is decremented by 1. The relative order of the other elements
in the sequence is unchanged. The return value is an iterator to the
element immediately following the one that was erased.

Warning: there is no guarantee that a valid iterator on a is still valid
after an insertion or an erasure. In some cases iterators do remain
valid, and in other cases they do not. The details are different for
each sequence class.

--
There are two things that simply cannot be doubted. Logic and our
ability to sense the world around us. Doubt those, and you no longer
have anyone to discuss it with, nor any ability to discuss it.
Sep 17 '06 #3

P: n/a
In article <ee**********@cache.uni-koblenz.de>, br******@uni-koblenz.de
says...
Hi.

Is there a possibility to delete elements out of a vector?
Yes. vector::erase can do that.
At the moment i do it by copying all needed elements to another vector.
After clearing the old vector i copy them back element by element.
The usual way is to use std::remove_if, which, contrary to its name
doesn't really remove anything -- it just rearranges the elements so
those to be kept are together at the beginning of the vector (or
whatever collection you're using). You can then use vector::erase to
erase the elements at the end that you don't want anymore.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 17 '06 #4

P: n/a
Jerry Coffin wrote:
In article <ee**********@cache.uni-koblenz.de>, br******@uni-koblenz.de
says...
Hi.

Is there a possibility to delete elements out of a vector?

Yes. vector::erase can do that.
At the moment i do it by copying all needed elements to another vector.
After clearing the old vector i copy them back element by element.

The usual way is to use std::remove_if, which, contrary to its name
doesn't really remove anything -- it just rearranges the elements so
those to be kept are together at the beginning of the vector (or
whatever collection you're using). You can then use vector::erase to
erase the elements at the end that you don't want anymore.
To remove items by value, std::remove() would be a better choice than
std::remove_if(). Also some sample code illustrating this technique
might be helpful:

#include <vector>
#include <algorithm>

int main()
{
std::vector<intv;

v.push_back(1);
v.push_back(2);
v.push_back(1);
v.push_back(3);
v.push_back(4);
v.push_back(5);

v.erase( remove( v.begin(), v.end(), 1), v.end());
}

would erase all the "1"'s from the vector v.

Greg

Sep 17 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.