std::basic_..._view::copy(3)
π NAME π
std::basic_string_view::copy β std::basic_string_view::copy (yes bestie, the name is the name, she's self-titled and iconic π
).
hey bestie π welcome to the std::basic_..._view::copy(3) page from the C++ Standard Libary. We're gonna get you all the way together on this member function, fr fr.
β¨ Synopsis β¨
size_type copy( CharT* dest, size_type count, size_type pos = 0 ) const;
(since C++17)β this is the C++17-and-later era, periodt.(constexpr since C++20)β and it got theconstexprglow-up in 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().
bestie, ifposis bigger thansize(), the vibe check fails and you getstd::out_of_rangeπ.
β‘ Complexity β‘
Linear in rcount.
π Example π
// Run this code
#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';
}
}
π¬ Output π¬
ABCD
BCDE
CDEF
basic_string_view::copy: __pos (which is 666) > __size (which is 6)
π See also π
substrβ returns a substring (public member function).copyβ copies characters (public member function ofstd::basic_string<CharT,Traits,Allocator>).copyβ copies a range of elements to a new location.copy_ifβ (function template) (C++11).memcpyβ copies one buffer to another (function).
signed, sealed, delivered by std::basic_..._view::copy(3) from http://cppreference.com, dated 2024.06.10 πβ¨
π¦ 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