std::basic_string::copy(3)
π std::basic_string::copy(3) β C++ Standard Libary π
πΈ NAME πΈ
std::basic_string::copy β std::basic_string::copy (yes bestie, it really said it twice π
)
π¦ Synopsis π¦
okay so here's the signature, copied exactly:
size_type copy( CharT* dest, size_type count, size_type pos
(constexpr since C++20)
= 0 ) const;
This one copies a substring [pos, pos + count) to the 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 π β no sneaky \0, bestie.
π 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. That's the whole vibe, periodt.
π¨ 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, icon behavior β¨.
π₯ Complexity π₯
Linear in count. No cap.
π Example π
The example, with its // Run this code note:
#include <iostream>
#include <string>
int main()
{
std::string foo("WINE");
// brace-initialization initializes all characters to 0,
// providing a null-terminator
char bar[4]{};
// do not copy the last char, to guarantee null-termination
foo.copy(bar, sizeof bar - 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.
LWG 847β applied toC++98; behavior as published: there was no exception safety guarantee; correct behavior: added strong exception safety guarantee.
π See also π
substrβ returns a substring (public member function)copyβ copies characters (public member function ofstd::basic_string_view<CharT,Traits>)copyβ copies a range of elements to a new locationcopy_ifβ (function template) (C++11)memcpyβ copies one buffer to another (function)
source/version + date:cppreference.comβ2024.06.10β mwah, bye bestie ππ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms