for an exercise i really need to know how to insert one vector into another.
here is an example of what i need:
//let's say i have these 2 vecs:
vec1 = { 18, 192, 34};
vec2 = { 171, 11, 50, 6};
and i choose the point where to connect them, lets say i choose vec 1 to be in place 2 of vector 2 like so:
result_vec = { 171, vec1, 50, 6};
so what i will actually get when i'm done is:
result_vec = { 171, 18, 192, 34, 50, 6};
for me it will be even better to see two examples, a simple example and another example with smart pointers like shared_ptr or unique_ptr.
Thanks!
It seems you want to substitute one element of one vector for elements of another vector. If so then you can write a function similar to that shown in the demonstrative program.
#include <iostream>
#include <vector>
#include <iterator>
template <typename T>
std::vector<T> & replace( std::vector<T> &v1,
typename std::vector<T>::size_type pos,
const std::vector<T> &v2 )
{
if ( not ( pos < v1.size() ) )
{
v1.insert( std::end( v1 ), std::begin( v2 ), std::end( v2 ) );
}
else
{
v1.insert( v1.erase( std::next( v1.begin(), pos ) ),
std::begin( v2 ), std::end( v2 ) );
}
return v1;
}
int main()
{
std::vector<int> v1 = { 171, 11, 50, 6 };
std::vector<int> v2 = { 18, 192, 34 };
for ( const auto &item : replace( v1, 1, v2 ) ) std::cout << item << ' ';
std::cout << '\n';
return 0;
}
The program output is
171 18 192 34 50 6