I think the simplest way is to use std::swap with the element with the given index and the element that preceds it.
For the first element you can use
std::swap( v.front(), v.back() );
Here is an example
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<char> v = { 'a', 'b', 'c', 'd', 'e', 'f' };
for ( char c : v ) std::cout << c << ' ';
std::cout << std::endl;
for ( size_t i : { 1, 3, 5 } )
{
if ( i == 0 ) std::swap( v.front(), v.back() );
else if ( i < v.size() ) std::swap( v[i], v[i-1] );
}
for ( char c : v ) std::cout << c << ' ';
std::cout << std::endl;
return 0;
}
The output is
a b c d e f
b a d c f e
If you do not want to rotate the vector then you can sibstitute the if statement for the following
for ( size_t i : { 1, 3, 5 } )
{
if ( 0 < i && i < v.size() ) std::swap( v[i], v[i-1] );
}
std::swap?