473,385 Members | 1,474 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,385 software developers and data experts.

q: STL vector manipulation

Hi. I have a STL vector of some relativly complicated objects

I was wondering if there was a good way to remove objects from the
middle of the vector.

Currently the only way I know how to remove element i is:

1. set the contents of the object at index i to the contents of the
last object
2. vector.pop_back();

but this is obviously annoying if my objects are not just simple
structs.

Thanks!

Oliver

Jul 23 '05 #1
5 1453
Ben
On 2005-05-26 17:31:10 -0400, "laniik" <lan...@yahoo.com> said:
Hi. I have a STL vector of some relativly complicated objects I was wondering if there was a good way to remove objects from the
middle of the vector.


vec.erase(iter);

where vec is your vector, and iter is an iterator to the object you
wish to remove.
--
Clark S. Cox, III
clarkc...@gmail.com
Or you can do vec.erase(vec.begin() + i) // where i is an integer
for the element #

If you are doing a lot of deleting from the middle then you may not
want to use vectors. A list might be better for you, in which case,
you must use the erase that Clark suggested.

-Benjamin Dacko

Jul 23 '05 #2
Ben wrote:
vec.erase(iter);

where vec is your vector, and iter is an iterator to the object you
wish to remove.
--
Clark S. Cox, III
clarkc...@gmail.com
Or you can do vec.erase(vec.begin() + i) // where i is an integer
for the element #

Which is the same, an iterator to the object.
--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #3
great thanks a lot

oliver

Jul 23 '05 #4

"laniik" <la****@yahoo.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
Hi. I have a STL vector of some relativly complicated objects

I was wondering if there was a good way to remove objects from the
middle of the vector.


You can do

v.erase(remove(v.begin(), v.end(), value), v.end());

which basically translates to "copy all the non-values towards the beginning
of the vector and erase the crud at the end".

So if you had a vector of int's, and there were 16 5's scattered over the
vector, then

v.erase(remove(v.begin(), v.end(), 5), v.end());

would make 1 pass over the vector, compacting all the non-5's and erasing 16
elements at the end.

Stephen Howe
Jul 23 '05 #5
Ben


Ben wrote:
vec.erase(iter); where vec is your vector, and iter is an iterator to the object you
wish to remove.
--
Clark S. Cox, III
clarkc...@gmail.com
Or you can do vec.erase(vec.begin() + i) // where i is an integer
for the element #


Which is the same, an iterator to the object.

--
Ioannis Vranos
http://www23.brinkster.com/noi*cys

Yes, it is an iterator.
I just wanted to show laniik how to do an erase using the same index
that he was using in his step 1.

-Benjamin Dacko

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sean Dettrick | last post by:
Hi, I have several <vector>'s of the same length, with entries as follows: I= A= B= I want to use STL to make (I == 0) a mask to operate on the elements of A and B, i.e. I want to do this:
27
by: Jason Heyes | last post by:
To my understanding, std::vector does not use reference counting to avoid the overhead of copying and initialisation. Where can I get a reference counted implementation of std::vector? Thanks.
9
by: vijetha | last post by:
Hi, I have been trying to store a string into a vector but I am not able to figure out how to do it. I am able to use vectors for storing integers but not able to use them fro strings. I want to...
2
by: neurojim | last post by:
Hi, a question probably posed many times butI couldn't find on the web... 1) i gather that for a vector, v, if v.size() exceeds v.capacity() during a call to v.push_back(x) the allocation of...
1
by: razilon | last post by:
Hi, I've written a managed class that makes use of stl vectors of a few unmanaged structs for data handling/manipulation, but I'm getting a few very strange errors. I get an "Unhandled...
0
by: razilon | last post by:
Hi, I've written a managed class that makes use of stl vectors of a few unmanaged structs for data handling/manipulation, but I'm getting a few very strange errors. I get an "Unhandled...
3
by: kistan | last post by:
Hi, First of all I am a beginner in STL. Please apologies for any stupid mistakes! I have a created vector<double> xpos which stores double numbers. In my vector xpos, xpos, xpos, xpos have...
0
by: L'eau Prosper Research | last post by:
Press Release: L'eau Prosper Research (Website: http://www.leauprosper.com) releases new TradeStation 8 Add-on - L'eau Prosper Market Manipulation Profiling Tools Set. L'eau Prosper Market...
0
by: L'eau Prosper Research | last post by:
NEW TradeStation 8 Add-on - L'eau Prosper Market Manipulation Profiling Tools Set By L'eau Prosper Research Press Release: L'eau Prosper Research (Website: http://www.leauprosper.com) releases...
1
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: 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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.