std::basic_string_view<CharT,Traits>::copy - cppreference.com

From cppreference.com

size_typecopy(CharT*dest,size_typecount,size_typepos=0)const;(since C++17)
(constexpr since C++20)Copies the substring [pos, pos+rcount) to the character array pointed to by dest, where rcount is the smaller of count and size()-pos.

Equivalent to Traits::copy(dest,data()+pos,rcount).

Parameters

dest - pointer to the destination character string count - requested substring length pos - position of the first character Return value

Number of characters copied.

Exceptions

std::out_of_range

if pos>size().

Complexity

Linear in rcount.

Example

Run this code

#include<array>#include<cstddef>#include<iostream>#include<stdexcept>#include<string_view>intmain(){constexprstd::basic_string_view<char>source{"ABCDEF"};std::array<char,8>dest;std::size_tcount{},pos{};dest.fill('\0');source.copy(dest.data(),count=4);// pos = 0std::cout<<dest.data()<<'\n';// ABCDdest.fill('\0');source.copy(dest.data(),count=4,pos=1);std::cout<<dest.data()<<'\n';// BCDEdest.fill('\0');source.copy(dest.data(),count=42,pos=2);// ok, count -> 4std::cout<<dest.data()<<'\n';// CDEFtry{source.copy(dest.data(),count=1,pos=666);// throws: pos > size()}catch(std::out_of_rangeconst&ex){std::cout<<ex.what()<<'\n';}}Output:

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