473,403 Members | 2,270 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,403 software developers and data experts.

adding/removing elements from std::vector

I have a std::vector<int> which, after some initialization, has a
fixed number of elements...after initialization I must do the
following repeatedly: I remove an element which could be anywhere in
the vector, and add another element which will always be at the end,
ie.

vector<int> v;
int i, x;

.... initialization

v.erase( v.begin() + i );
v.push_back( x );

My question is whether there is a better way to do this...since I know
that there will always be a sequence of removing an element followed
by appending an element to the end, can I do it in a way which doesn't
use either erase or push_back? Or are most implementations such that
this shouldn't be a problem? Since this erase/push_back sequence will
happen many times, I would like to avoid any possible memory
management since the size of the vector will never really change.

Regards,
Tino
Jul 22 '05 #1
1 2282

"Tino" <ti****@yahoo.com> wrote in message
news:f9**************************@posting.google.c om...
I have a std::vector<int> which, after some initialization, has a
fixed number of elements...after initialization I must do the
following repeatedly: I remove an element which could be anywhere in
the vector, and add another element which will always be at the end,
ie.

vector<int> v;
int i, x;

... initialization

v.erase( v.begin() + i );
v.push_back( x );

My question is whether there is a better way to do this...since I know that there will always be a sequence of removing an element followed
by appending an element to the end, can I do it in a way which doesn't use either erase or push_back? Or are most implementations such that this shouldn't be a problem? Since this erase/push_back sequence will happen many times, I would like to avoid any possible memory
management since the size of the vector will never really change.


Iterators pointing to poisitions before the removed element are
required not to be invalidated by removal, which means that the
removal will not cause any deallocation and reallocation of the
vector's underlying storage. When you immediately append another
element, it should be occupy an already allocated position.

The main expense is the assignments required to effecively shift the
elements above the removal point down one index. This is what you
might want to avoid, perhaps by using a list, depending on your other
requirements.

Jonathan
Jul 22 '05 #2

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

Similar topics

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.
6
by: Jason Heyes | last post by:
What is a good way of removing elements from std::vector so that the elements removed satisfy a predicate and end up stored in another std::vector. It seems as though the algorithm std::remove_if...
17
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling...
8
by: Jason Heyes | last post by:
Does the STL have a function like this one? template <typename T> void remove(std::vector<T> &v, std::vector<T>::size_type index) { std::swap(v, v.back()); v.resize(index); } Unlike...
32
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: ...
56
by: Peter Olcott | last post by:
I am trying to refer to the same std::vector in a class by two different names, I tried a union, and I tried a reference, I can't seem to get the syntax right. Can anyone please help? Thanks
9
by: aaragon | last post by:
I am trying to create a vector of type T and everything goes fine until I try to iterate over it. For some reason, the compiler gives me an error when I declare std::vector<T>::iterator iter;...
13
by: jubelbrus | last post by:
Hi I'm trying to do the following. #include <vector> #include <boost/thread/mutex.hpp> #include <boost/shared_ptr.hpp> #include <boost/tuple/tuple.hpp> class {
3
by: MacApp | last post by:
I read in http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#69 they proposed: My question is: does the above implies the: (Type *) &v<char>==(Type *)(&v<char> + n*sizeof (Type));...
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: 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...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.