appending one vector after another.

T

toton

Hi,
I want to append one vector after another.
so like,
vector<int> v1; ///put some elements to v2.
I have a second vector
vector<int> v2; ///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?
 
M

Mark P

toton said:
Hi,
I want to append one vector after another.
so like,
vector<int> v1; ///put some elements to v2.
I have a second vector
vector<int> v2; ///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());
 
P

peter koch

Mark said:
toton said:
Hi,
I want to append one vector after another.
so like,
vector<int> v1; ///put some elements to v2.
I have a second vector
vector<int> v2; ///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
 
P

peter koch

toton said:
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
 
T

toton

peter said:
Mark said:
toton said:
Hi,
I want to append one vector after another.
so like,
vector<int> v1; ///put some elements to v2.
I have a second vector
vector<int> v2; ///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
 
J

Jens Theisen

toton said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top