• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

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

    履歴 編集

    function template
    <algorithm>

    std::ranges::copy_n

    namespace std::ranges {
      template <input_iterator I,
                weakly_incrementable O>
        requires indirectly_copyable<I, O>
      constexpr copy_n_result<I, O>
        copy_n(I first,
               iter_difference_t<I> n,
               O result); // (1) C++20
    }
    

    概要

    指定された数の要素をコピーする。

    効果

    0 以上 n 未満であるそれぞれの i について、*(result + i) = *(first + i) を行う。

    戻り値

    copy_n_result {
      .in  = first + n,
      .out = result + n,
    }
    

    計算量

    正確に n 回代入が行われる。

    #include <algorithm>
    #include <iostream>
    #include <iterator>
    #include <vector>
    
    int main() {
      std::vector<int> v = { 3, 1, 5, 2, 4 };
      std::ranges::copy_n(v.begin(), 5, std::ostream_iterator<int>(std::cout, "\n"));
    }
    

    出力

    3
    1
    5
    2
    4
    

    バージョン

    言語

    • C++20

    処理系

    参照