Namespaces
Variants

std::basic_string_view<CharT,Traits>::copy

cppreference.netより
 
 
 
 
size_type copy( CharT* dest, size_type count, size_type pos = 0 ) const;
(C++17以降)
(C++20以降constexpr)

部分文字列 [pos, pos + rcount) を、dest が指す文字配列にコピーします。ただし、rcount は count と size() - pos のうち小さい方です。

以下と同等です: Traits::copy(dest, data() + pos, rcount)。

パラメータ

dest - 宛先文字列へのポインタ
count - 要求された部分文字列の長さ
pos - 最初の文字の位置

戻り値

コピーされた文字数。

例外

std::out_of_range posが pos > size ( ) の場合。

計算量

rcountに対して線形。

例

#include <array>
#include <cstddef>
#include <iostream>
#include <stdexcept>
#include <string_view>
int main()
{
    constexpr std::basic_string_view<char> source{"ABCDEF"};
    std::array<char, 8> dest;
    std::size_t count{}, pos{};
    dest.fill('\0');
    source.copy(dest.data(), count = 4); // pos = 0
    std::cout << dest.data() << '\n'; // ABCD
    dest.fill('\0');
    source.copy(dest.data(), count = 4, pos = 1);
    std::cout << dest.data() << '\n'; // BCDE
    dest.fill('\0');
    source.copy(dest.data(), count = 42, pos = 2); // ok, count -> 4
    std::cout << dest.data() << '\n'; // CDEF
    try
    {
        source.copy(dest.data(), count = 1, pos = 666); // throws: pos > size()
    }
    catch (std::out_of_range const& ex)
    {
        std::cout << ex.what() << '\n';
    }
}

出力:

ABCD
BCDE
CDEF
basic_string_view::copy: __pos (which is 666) > __size (which is 6)

関連項目

部分文字列を返す
(パブリックメンバ関数)
文字をコピーする
(std::basic_string<CharT,Traits,Allocator>のパブリックメンバ関数)
要素の範囲を新しい位置にコピーする
(関数テンプレート & アルゴリズム関数オブジェクト)
バッファを別のバッファにコピーする
(関数)