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::ranges::copy_backward - cpprefjp C++日本語リファレンス

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

最終更新日時: 2026年09月19日 21時31分51秒 (JST)
rotarymars が更新

履歴 編集

function template
<algorithm>

std::ranges::copy_backward(C++20)

namespace std::ranges {
  template <bidirectional_iterator I1,
            sentinel_for<I1> S1,
            bidirectional_iterator I2>
    requires indirectly_copyable<I1, I2>
  constexpr copy_backward_result<I1, I2>
    copy_backward(I1 first, S1 last, I2 result); // (1) C++20

  template <bidirectional_range R,
            bidirectional_iterator I>
    requires indirectly_copyable<iterator_t<R>, I>
  constexpr copy_backward_result<borrowed_iterator_t<R>, I>
    copy_backward(R&& r, I result);              // (2) C++20
}

概要

指定された範囲の要素を後ろからコピーする。

  • (1): イテレータ範囲を指定する
  • (2): Rangeを直接指定する

事前条件

result は (first,last] の範囲に含まれてはならない。

効果

[first,last) 内にある要素を、それぞれ [result - (last-first),result) へコピーする。

コピーは last - 1 から順番に行い、1 以上 last - first 以下であるそれぞれの n について、*(result - n) = *(last - n) を行う。

戻り値

copy_backward_result {
  .in  = last,
  .out = result - (last - first),
}

計算量

正確に last - first 回代入が行われる。

備考

last が [result - (last-first), result) の範囲内にあるときには、copy() の代わりに copy_backward() を使うべきである。

例

#include <algorithm>
#include <iostream>
#include <list>
#include <iterator>

int main() {
  std::list<int> ls = { 1,2,3,4,5 };
  // 1,2,3 の範囲を、3,4,5 の値のある範囲へコピーする
  std::ranges::copy_backward(ls.begin(), std::next(ls.begin(), 3), ls.end());

  // 以下のコードだと期待した結果にならないことを確認しよう
  // std::ranges::copy(ls.begin(), std::next(ls.begin(), 3), std::next(ls.begin(), 2));

  std::ranges::copy(ls.begin(), ls.end(), std::ostream_iterator<int>(std::cout, ","));
}

出力

1,2,1,2,3,

バージョン

言語

  • C++20

処理系

  • Clang: 16.0.6 ✅
  • GCC: 10.1.0 ✅
  • Visual C++: 2019 Update 10 ✅

参照

  • N4861 25 Algorithms library
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.