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

  1. リファレンス
  2. string_view
  3. basic_string_view
  4. copy

最終更新日時: 2026年10月03日 22時31分04秒 (JST)
Akira Takahashi が更新

履歴 編集

function
<string_view>

std::basic_string_view::copy(C++17)

size_type copy(CharT* s, size_type n, size_type pos = 0) const;           // C++17

constexpr size_type copy(CharT* s, size_type n, size_type pos = 0) const; // C++20

概要

他の文字列に、自身の文字列をコピーする。

要件

nとsize() - posのうち、小さい方をrlenとする。

  • [s, s + rlen)が妥当な範囲であり、その範囲内の要素にアクセスできること

効果

以下と等価の処理を行う:

Traits::copy(s, data() + pos, rlen);

戻り値

rlen

備考

この関数は、例外を送出しうるため、フリースタンディング処理系では削除される(フリースタンディング処理系では使用できない)。

例外

pos > size()の場合、std::out_of_range例外を送出する。

例

#include <iostream>
#include <string_view>

int main()
{
  const std::string_view sv = "hello";

  // 全体をコピーする
  {
    char result[5 + 1] = {};
    sv.copy(result, 5);

    std::cout << result << std::endl;
  }

  // 先頭3要素だけコピーする
  {
    char result[3 + 1] = {};
    sv.copy(result, 3);

    std::cout << result << std::endl;
  }

  // 2番目以降の要素をコピーする
  {
    char result[3 + 1] = {};
    sv.copy(result, 3, 2);

    std::cout << result << std::endl;
  }
}

出力

hello
hel
llo

バージョン

言語

  • C++17

処理系

  • Clang: 4.0 ✅
  • GCC: 7.1 ✅
  • Visual C++: ??

参照

  • P1032R1 Misc constexpr bits
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.