473,473 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

appending one vector after another.

Hi,
I want to append one vector after another.
so like,
vector<intv1; ///put some elements to v2.
I have a second vector
vector<intv2; ///it has some elements.

Now I do
v1.reserve(v1.size() + v2.size()) ;
and then
vector<int>::const_iterator begin = v2.begin();
vector<int>::const_iterator end = v2.end();
for( ; begin!=end; ++begin){
v1.push_back(*begin);
}
Is there any better way to do it?
Similarly I need to do pop_front from a deque a number of elements. So
using pop_front in a loop is the best way?

Oct 5 '06 #1
5 5541
toton wrote:
Hi,
I want to append one vector after another.
so like,
vector<intv1; ///put some elements to v2.
I have a second vector
vector<intv2; ///it has some elements.

Now I do
v1.reserve(v1.size() + v2.size()) ;
and then
vector<int>::const_iterator begin = v2.begin();
vector<int>::const_iterator end = v2.end();
for( ; begin!=end; ++begin){
v1.push_back(*begin);
}
Is there any better way to do it?
v1.reserve( v1.size() + v2.size());
v1.insert( v1.end(), v2.begin(), v2.end());

Oct 5 '06 #2

Mark P wrote:
toton wrote:
Hi,
I want to append one vector after another.
so like,
vector<intv1; ///put some elements to v2.
I have a second vector
vector<intv2; ///it has some elements.

Now I do
[snip]
Is there any better way to do it?

v1.reserve( v1.size() + v2.size());
v1.insert( v1.end(), v2.begin(), v2.end());
The reserve should not be needed in any decent implementation - the
implementation will do it for you (and you will reduce your program
size with 50%).

/Peter

Oct 5 '06 #3

toton wrote:
Hi,
[snip]
Similarly I need to do pop_front from a deque a number of elements. So
using pop_front in a loop is the best way?
(I've snipped the vector question as it was answered by Mark.)

Loops are often not the way when using the standard library. Here you
use

std::deque::erase(iterator first, iterator last) (where first is
deque::begin in your case).

/Peter

Oct 5 '06 #4

peter koch wrote:
Mark P wrote:
toton wrote:
Hi,
I want to append one vector after another.
so like,
vector<intv1; ///put some elements to v2.
I have a second vector
vector<intv2; ///it has some elements.
>
Now I do
[snip]
Is there any better way to do it?
v1.reserve( v1.size() + v2.size());
v1.insert( v1.end(), v2.begin(), v2.end());

The reserve should not be needed in any decent implementation - the
implementation will do it for you (and you will reduce your program
size with 50%).
Thanks this is what I was looking for.
MS STL implementation do the reserve before inserting using
std::distance . Thus that line is not needed. What I was interested to
check that, insert at middle and inserting at end has a big difference
in terms of copying objects. Whether insert handles that. It looks that
it check that point.
In actual case, the container is not exactly a vector, but a container
of my own! So I am going to provide the insert functionality (little
modified, append functionality, it can insert only at end ).
A second related question.
In my container class (say it is a vector/ deque kind of thing, except
elements are allowed to push_back only, and it automatically do
pop_front when buffer is full. It is kind of a circular buffer with
conditional append/ push_back where one can say whether it wants to
increase size if memory is exhausted or not. Just to prevent removing
element from a single append operation. Say the buffer has a capacity
of 500 elements. And appending 510 element will not remove the 10
elements from the back in the insert operation, rather will resize it.
However two append of 300 elements may remove the total 300 elements
from first insert to make space for the second).
Now, I want user to get a pair of iterator for a particular append (
you can think it as some sort of collection of collection). or say for
some from-to location. Do you think it is ok to return this kind of
pair (My question is due to that STL usually returns begin & end
separately, while I want to return a std::pair with from & to from the
container itself).

Thanks
/Peter
Oct 5 '06 #5
"toton" <ab*******@gmail.comwrites:
Is there any better way to do it?
>
v1.reserve( v1.size() + v2.size());
v1.insert( v1.end(), v2.begin(), v2.end());
The reserve should not be needed in any decent implementation - the
implementation will do it for you (and you will reduce your program
size with 50%).
Thanks this is what I was looking for.
You might also fancy:

back_insert_iterator< vector< int bi(v1);
copy(v2.begin(), v2.end(), bi);

though frankly I don't. :)

(vector only needs push_back for this)
Now, I want user to get a pair of iterator for a particular append (
you can think it as some sort of collection of collection). or say for
some from-to location. Do you think it is ok to return this kind of
pair (My question is due to that STL usually returns begin & end
separately, while I want to return a std::pair with from & to from the
container itself).
Returing a pair of iterators is not so uncommon, though the STL does
not do it. Note that there is a concept in boost called range, and
that a pair of iterators models this.

--
Gruß, Jens
Oct 5 '06 #6

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

Similar topics

1
by: dmiller23462 | last post by:
Hey guys.... I put an error-handling in my page and have it posted at the complete end of the code, see below(when people were putting in 's I was getting the delimiter errors). Great, I...
10
by: Stefan Höhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to...
9
by: luigi | last post by:
Hi, I am trying to speed up the perfomance of stl vector by allocating/deallocating blocks of memory manually. one version of the code crashes when I try to free the memory. The other version...
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.
11
by: Steve | last post by:
Hi, I'm using a std::vector to store a list of user defined objects. The vector may have well over 1000 elements, and I'm suffering a performance hit. If I use push_back I get a much worse...
18
by: John Black | last post by:
Hi, I am not familiar with for_each very well, suppoase I have a vector<pair<unsigned int, unsigned int> > vec1 and the contents are {<0x00000000, 0x000000FF>, <0x10000000, 0x2FFFFFFF>} what...
6
by: Matthias | last post by:
Hi, say I have a vector v1: std::vector<SomeType> v1; and I need a vector v2 of pointers to v1's elements: std::vector<SomeType*> v2;
5
by: flavourofbru | last post by:
Hi, Is it possible to add an int or a string or a char to a vector variable?? For example if I declare a vector variable as follows: vector<string> abc; Can I append an integer value or some...
13
by: prasadmpatil | last post by:
I am new STL programming. I have a query regarding vectors. If I am iterating over a vector using a iterator, but do some operations that modify the size of the vector. Will the iterator recognize...
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
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...
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
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.