1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
#include <list>
#include <deque>
#include <vector>
#include <iterator>
#include <algorithm>
#include <type_traits>
#define IsList(V, T) std::is_same< V<T>, std::list<T> >::value
#define IsDeque(V, T) std::is_same< V<T>, std::deque<T> >::value
#define IsVector(V, T) std::is_same< V<T>, std::vector<T> >::value
//---------------------------------------------------------------------------
template <template <typename> class V, typename T> void VectorMove(V <T> & Vector, int FromIndex, int ToIndex)
{
static_assert(IsList(V, T) || IsDeque(V, T) || IsVector(V, T), "Vector must be a container type.");
class V<T>::iterator it1, it2, it3;
it1 = it2 = it3 = Vector.begin();
std::advance(it1, FromIndex);
std::advance(it2, FromIndex + 1);
if (FromIndex > ToIndex)
{
std::advance(it3, ToIndex);
std::rotate(it3, it1, it2);
}
else
{
std::advance(it3, ToIndex + 1);
std::rotate(it1, it2, it3);
}
}
//---------------------------------------------------------------------------
void zTest()
{
std::vector <int> qwe(5, 0);
VectorMove(qwe, 0, 1);
}
//---------------------------------------------------------------------------
| |