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 > 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)

참고 항목

부분 문자열을 반환합니다
(public member function)
문자를 복사합니다
( std::basic_string<CharT,Traits,Allocator> 의 public member function)
요소들의 범위를 새로운 위치로 복사합니다
(function template)
한 버퍼를 다른 버퍼로 복사합니다
(function)