So, I've been doing this when concatenating two collections (say, a std::vector
with a std::array
):
std::vector<Foo> foos;
std::array<Foo,12> toAdd = { /* value list */ };
for(....)
{
foos.insert(end(foos),begin(toAdd),end(toAdd));
}
but, it just occurred to me to do this:
template<typename T1, typename T2>
void append(T1 &target, T2 &source)
{
target.insert(end(target),begin(source),end(source));
}
/// ....
append(foos,toAdd);
Is there an equivalent routine already in the STL that I haven't run across?
In this case, the toAdd
array is initialized with a specific pattern of values that is difficult to generate in code, and is a bit lengthy. Using the array initializer instead of a lot of ::push_back()
calls is much clearer in code. Otherwise, I'd just vector::reserve()
to up the size, and put everything directly into the vector.