Vector question

Hi,

Is there any way to add multiple elements to a vector at once?

Like you can do:
int arrayname = {element1, element2, element3};

Is this possible with vectors? I've tried some combinations, but none of them seems to work.

Thanks
Not with the current C++ version but you can do it if the values are all the same:
vector<char> vec ( 100, 'a' ); will create a vector containing 100 'a'
Well, the values aren't the same...

So it's impossible to do that? :(
vector<> has an insert() method that takes two iterators which define a range. For example:

1
2
3
4
5
6
7
int AnArray[ 5 ] = { 1, 3, 5, 7, 9 };

vector<int> v;

v.insert( v.end(), AnArray, AnArray + 5 );

// v now contains 5 elements: 1, 3, 5, 7, and 9. 


Note: vector<> also contains a constructor that takes two iterators.
Topic archived. No new replies allowed.