c++c++11vectorinsertinitializer-list

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});

Solution

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

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