Please review my answer for this interview question:
#include <iostream>
#include <vector>
std::vector<int> merge2Sorted ( std::vector<int> left, std::vector<int> right )
{
//finger matching algo
auto itLeft = left.begin();
auto itRight = right.begin();
auto itLeftEnd = left.end();
auto itRightEnd = right.end();
std::vector<int> result;
result.reserve(std::max(left.size(),right.size()));
while ( itLeft != itLeftEnd &&
itRight != itRightEnd )
{
if ( *itLeft < *itRight )
result.push_back( *itLeft++ );
else
result.push_back( *itRight++ );
}
// copy rest of left array
while ( itLeft != itLeftEnd )
result.push_back(*itLeft++);
// copy rest of right array
while ( itRight != itRightEnd )
result.push_back(*itRight++);
return result;
}
int main ()
{
std::vector<int> v1 = { 1,2,3,4,5,6,7,8,9,10 };
std::vector<int> v2 = { -1,-2,0,3,7,9,11,12 };
std::vector<int> v3 ( merge2Sorted ( v1, v2 ) );
for ( auto& i: v3 )
std::cout << i << std::endl;
}