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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| template<class IT>
void iteriter_rotate_right(IT first, IT last)
{
--last; // check range [first,last[ has at least 2 elements !
typename IT::value_type::value_type tmp = std::move(**last);
do
{
**last = std::move(**(last - 1));
} while (--last != first);
**first = std::move(tmp);
}
template<unsigned D, class It>
class delayed_mover
{
std::array<It, D> vi;
unsigned vicnt{};
void rotate_unchecked()
{
iteriter_rotate_right(vi.begin(), vi.begin() + vicnt);
}
public:
void push_unchecked(It i) noexcept
{
vi[vicnt++] = i;
}
void rotate()
{
if (vicnt > 1)
rotate_unchecked();
vicnt = 0;
}
void push(It i)
{
if (D == vicnt)
{
rotate_unchecked();
vicnt = 1;
}
vi[vicnt++] = i;
}
};
int main()
{
std::vector<std::string> v{ "zéro","un","deux","trois","quatre","cinq","six","sept","huit","neuf" };
delayed_mover<5, std::vector<std::string>::iterator> dr;
std::vector<std::string>::iterator it = v.begin();
dr.push_unchecked(++it);
dr.push(++it);
dr.push(++it);
dr.push(++it);
dr.push(++it);
dr.push(++it);
dr.push(++it);
dr.push(++it);
dr.rotate();
return 0;
} |
Partager