Shifting/aligning/rotating a circular buffer to zero in-place

前端 未结 5 1653
谎友^
谎友^ 2021-02-02 00:13

I\'m using a circular buffer to push data onto either end of a list. After I\'m done I want to align the buffer so the first element in the list is at position zero and can be u

5条回答
  •  心在旅途
    2021-02-02 01:10

    This algorithm taken from the std::rotate implementation on cplusplus.com is quite nice:

    template 
      void rotate (ForwardIterator first, ForwardIterator middle,
                   ForwardIterator last)
    {
      ForwardIterator next = middle;
      while (first!=next)
      {
        swap (*first++,*next++);
        if (next==last) next=middle;
        else if (first==middle) middle=next;
      }
    }
    

    http://www.cplusplus.com/reference/algorithm/rotate/

提交回复
热议问题