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

From cppreference.com

size_typecopy(CharT*dest,size_typecount,size_typepos=0)const;(constexpr since C++20)Copies a substring [pos, pos+count) to character string pointed to by dest. If the requested substring lasts past the end of the string, or if count==npos, the copied substring is [pos,

size()

).

The resulting character string is not null-terminated.

Parameters

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

Number of characters copied.

Exceptions

std::out_of_range

if pos>size().

If an exception is thrown for any reason, this function has no effect (

strong exception safety guarantee

).

Complexity

Linear in count.

Example

Run this code

#include<iostream>#include<string>intmain(){std::stringfoo("WINE");// brace-initialization initializes all characters to 0,// providing a null-terminatorcharbar[4]{};// do not copy the last char, to guarantee null-terminationfoo.copy(bar,sizeofbar-1);std::cout<<bar<<'\n';// requires bar to be null-terminated}Output:

WIN Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior

LWG 847

C++98 there was no exception safety guarantee added strong exception safety guarantee See also