Is there a way to construct a vector
as the concatenation of 2 vector
s (Other than creating a helper function?)
For example:
I think you have to write a help function. I'd write it as:
std::vector concatenate(const std::vector& lhs, const std::vector& rhs)
{
auto result = lhs;
std::copy( rhs.begin(), rhs.end(), std::back_inserter(result) );
return result;
}
The call it as:
const auto concatenation = concatenate(first, second);
If the vectors are likely to be very large (or contain elements that are expensive to copy), then you might need to do a reserve
first to save reallocations:
std::vector concatenate(const std::vector& lhs, const std::vector& rhs)
{
std::vector result;
result.reserve( lhs.size() + rhs.size() );
std::copy( lhs.begin(), lhs.end(), std::back_inserter(result) );
std::copy( rhs.begin(), rhs.end(), std::back_inserter(result) );
return result;
}
(Personally, I would only bother if there was evidence it was a bottleneck).