move element from one vector to another c++ code example

Example 1: how to append one vector to another c++

vector<int> a;
vector<int> b;
// Appending the integers of b to the end of a 
a.insert(a.end(), b.begin(), b.end());

Example 2: copy a part of a vector in another in c++

// Copying vector by copy function 
copy(vect1.begin(), vect1.end(), back_inserter(vect2));

Example 3: shift element to end of vector c++

template <typename t> void move(std::vector<t>& v, size_t oldIndex, size_t newIndex)
{
    if (oldIndex > newIndex)
        std::rotate(v.rend() - oldIndex - 1, v.rend() - oldIndex, v.rend() - newIndex);
    else        
        std::rotate(v.begin() + oldIndex, v.begin() + oldIndex + 1, v.begin() + newIndex + 1);
}

auto initial_pos = 1;
auto final_pos = 4;
move(some_vector, initial_pos, final_pos);

Tags:

Cpp Example