std::string copy() method
- since C++20
- until C++20
// Const version only
constexpr size_type copy( CharT* dest, size_type count, size_type pos = 0 ) const;
// Const version only
size_type copy( CharT* dest, size_type count, size_type pos = 0 ) const;
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 stringcount
- length of the substringpos
- position of the first character to include
Return value
Number of characters copied.
Complexity
Linear in count
- O(count).
Exceptions
Throws std::out_of_range
if pos > size()
.
Example
#include <string>
#include <iostream>
int main()
{
std::string foo("quuuux");
char bar[7]{};
foo.copy(bar, sizeof bar);
std::cout << bar << '\n';
}
Output
quuuux
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.
Hover to see the original license.