Eric B
Eric B

Reputation: 4437

concat std::vector and initializer list

In c++11 you can do this wonderful syntax:

vector<int> numbers = {1, 2, 3};

Is there a way to concatenate a further initializer list onto an existing vector?

numbers.??? ({4, 5, 6});

or

std::??? (numbers, {4, 5, 6});

Upvotes: 27

Views: 10668

Answers (3)

Alex Reinking
Alex Reinking

Reputation: 19966

You can use std::vector::insert. Link to example code

#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> a = {1,2,3};
    a.insert(a.end(), {4,5,6});
    for(int &i : a) {
        cout << i << " ";
    }
    cout << endl;
    return 0;
}

Upvotes: 8

Pierre Fourgeaud
Pierre Fourgeaud

Reputation: 14510

You can use std::vector::insert for that:

#include <vector>

vector<int> numbers = {1, 2, 3};
numbers.insert( numbers.end(), {4, 5, 6} );

Upvotes: 32

masoud
masoud

Reputation: 56509

Use std::vector::insert:

numbers.insert(numbers.end(), {4, 5, 6});

Upvotes: 10

Related Questions