473,320 Members | 2,161 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Deleting elements out of a vector

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
4 2448
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Dan | last post by:
hello, I would to know if it is possible to delete an instance in an array, The following does not allow me to do a delete. I am trying to find and delete the duplicate in an array, thanks ...
3
by: Generic Usenet Account | last post by:
This posting is just for clarification of my understanding. It appears to me that only vector and deque iterators (i.e. random access iterators) allow "iterator arithmetic" operations (like...
5
by: FefeOxy | last post by:
Hi, > I'm having a debug assertion error within the file dbgdel.cpp with the expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) I traced the origin of the error and it happened as I tried to...
1
by: Varun Kacholia | last post by:
I apologize if there exists a standard way of deleting multiple elements from a STL hash_multiset (or even multiset for that matter) that I am unaware of. The problem, I see, with multisets is...
10
by: Sabiyur | last post by:
Hi All, I am coding as below. int *x = new int; int * y= x; ............ ............. del y; x=NULL;
7
nabh4u
by: nabh4u | last post by:
hi, i have a double linked list containing some elements and i have a vector which stores the address of the elements in the list (pointer to the list). i want to delete a value from the list,like...
16
by: kaferro | last post by:
What is the typical way to loop through a vector while deleting certain elements during the loop process? The code below works, but I am wondering if there is a better solution. ...
1
by: eusko | last post by:
for example vec = new int; then we input the values of vector elements if we decided to change the vector size, is it possible to do it without deleting the existing values of the vector...
3
by: deepak1905 | last post by:
Hi, I am working on c++ in a linux system ( Fedora core 4 ), kernel version - 2.6.11-1.1369_FC4 gcc version - 4.0.0 20050519 ( Red Hat 4.0.0-8 ) In my code i am creating a vector to store...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.