GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::copy_n - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. algorithm
  3. copy_n

最終更新日時: 2026年04月13日 15時17分49秒 (JST)
teruyamato0731 が更新

履歴 編集

function template
<algorithm>

std::copy_n(C++11)

namespace std {
  template <class InputIterator, class Size, class OutputIterator>
  OutputIterator
    copy_n(InputIterator first,
           Size n,
           OutputIterator result);   // (1) C++11

  template <class InputIterator, class Size, class OutputIterator>
  constexpr OutputIterator
    copy_n(InputIterator first,
           Size n,
           OutputIterator result);   // (1) C++20

  template <class ExecutionPolicy, class ForwardIterator1, class Size,
            class ForwardIterator2>
  ForwardIterator2
    copy_n(ExecutionPolicy&& exec,
           ForwardIterator1 first,
           Size n,
           ForwardIterator2 result); // (2) C++17
}

概要

イテレータ範囲[first, first + n) (範囲の先頭N個) の要素をコピーする。

効果

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

戻り値

result + n

計算量

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

例

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

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

出力

3
1
5
2
4

実装例

template<class InputIterator, class Size, class OutputIterator>
OutputIterator copy_n(InputIterator first, Size n, OutputIterator result) {
  for (Size i = 0; i < n; i++)
    *result++ = *first++;
  return result;
}

バージョン

言語

  • C++11

処理系

  • Clang: 3.0 ✅
  • GCC: 4.4.7 ✅
  • Visual C++: 2010 ✅, 2012 ✅, 2013 ✅, 2015 ✅

関連項目

  • ranges::copy_n

参照

  • N2569 More STL algorithms
  • N2666 More STL algorithms (revision 2)
  • P0202R3 Add Constexpr Modifiers to Functions in <algorithm> and <utility> Headers
  • P0467R2 Iterator Concerns for Parallel Algorithms
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.