Namespaces
Variants

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

From 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> の公開メンバ関数)
要素の範囲を新しい場所にコピーする
(関数テンプレート)
バッファを別のバッファにコピーする
(関数)