473,796 Members | 2,376 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2473
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**********@c ache.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**********@c ache.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**********@c ache.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<int v;

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
2482
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 for ( j =0; j<MAX ; j++) { for ( i =0; i<MAX ; i++)
3
7070
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 iter+2, iter-1 etc.). Kindly confirm. Thanks, Song
5
2782
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 delete a polymorphic class as follows:
1
2021
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 that you cannot obtain info to uniquely identify an element (eg: indices in a vector, or key in set/hash_set). Hence what I'd like to do is iterate over a multiset and delete elements while doing so. Now I did read somewhere that deleting an...
10
1579
by: Sabiyur | last post by:
Hi All, I am coding as below. int *x = new int; int * y= x; ............ ............. del y; x=NULL;
7
2244
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 the nth value enetered in the list, in constant time. how do i do that? i got the address of the element to be deleted from the vector but how do i relate that address with the element in the list and then delete it and rearrange the pointers for the...
16
2521
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. vector<intvTmp; vTmp.push_back(1); vTmp.push_back(2); vTmp.push_back(1); vTmp.push_back(2);
1
1411
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 elements? Thanks!
3
2680
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 pointers of type structure "SAMPLE_TABLE_STRUCT" ( size of this structure is 36 bytes ). I create an instance of structure "SAMPLE_TABLE_STRUCT" using operator "new" and push back into the vector,this is done inside a for loop for 204800 times. After i...
0
9680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10228
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10173
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7547
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6788
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.