• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

    最終更新日時(UTC):
    が更新

    履歴 編集

    function template
    <algorithm>

    std::ranges::rotate_copy

    namespace std::ranges {
      template <forward_iterator I,
                sentinel_for<I> S,
                weakly_incrementable O>
        requires indirectly_copyable<I, O>
      constexpr rotate_copy_result<I, O>
        rotate_copy(I first,
                    I middle,
                    S last,
                    O result); // (1) C++20
    
      template <forward_range R,
                weakly_incrementable O>
        requires indirectly_copyable<iterator_t<R>, O>
      constexpr rotate_copy_result<borrowed_iterator_t<R>, O>
        rotate_copy(R&& r,
                    iterator_t<R> middle,
                    O result); // (2) C++20
    }
    

    概要

    middleの要素が先頭、middle-1の要素が末尾となるように、[first,last)の要素の並びを回転させ、その結果を出力の範囲へコピーする。

    事前条件

    [first,last)[result,result + (last - first)) の範囲は重なっていてはならない。

    効果

    0 以上 last - first 未満の整数 i について、*(result + i) = *(first + (i + (middle - first)) % (last - first)) という操作によって [first,last) の範囲を [result,result + (last - first)) の範囲へコピーする

    戻り値

    回転前の先頭の要素を指すイテレータresult + (last - first)

    計算量

    正確に last - first 回代入する。

    #include <algorithm>
    #include <iostream>
    #include <string>
    #include <iterator>
    
    int main() {
      std::string str = "rotate";
      std::string result;
    
      std::ranges::rotate_copy(str, str.begin() + 2, std::back_inserter(result));
    
      std::cout << result << std::endl;
    }
    

    出力

    tatero
    

    バージョン

    言語

    • C++20

    処理系

    参照