0

Possible Duplicate:
C++: Appending a vector to a vector

I'm looking for unlimited container (one that I add elements at run time as much as I need as long as I have enough space),such as 'vector' or 'linked list' that has a methos that accepts another container and copies its elements.

Is there such a thing?

Community
  • 1
  • 1
kakush
  • 3,334
  • 14
  • 47
  • 68

2 Answers2

8

You can use std::copy and a back_inserter iterator to insert a range of values into any variable-size standard container.

std::vector also has an insert overload that accepts a range.

See Appending a vector to a vector, the answers are actually usable for containers besides just vectors.

Community
  • 1
  • 1
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
1

vector supports this functionality, go through the constructors:

template <class InputIterator>
     vector ( InputIterator first, InputIterator last, 
               const Allocator& = Allocator() );

And:

std::vector<int> source;  
//populate source
std::vector<int> dest(source.begin(),source.end());
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 3
    I think he wants to add into an existing container, not only at creation time. – Ben Voigt Jun 06 '12 at 14:36
  • but I dont want to create a new 'vector' every time I want to add elements. generally, I need to add to my vector, arrays.. – kakush Jun 06 '12 at 14:38
  • @kakush sorry, misunderstood the question. Won't edit as you already have the answer from Ben, but will leave it up as it can still be useful. – Luchian Grigore Jun 06 '12 at 15:11