GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::basic_string_view::copy(3) C++ Standard Libary std::basic_string_view::copy(3)

std::basic_string_view::copy - std::basic_string_view::copy


size_type copy( CharT* dest, size_type count, size_type pos = 0 ) (since C++17)
const; (until C++20)
constexpr size_type copy( CharT* dest, size_type count, (since C++20)
size_type pos = 0 ) const;


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).


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


Number of characters copied


std::out_of_range if pos > size().


Linear in rcount.

// 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';
}
}


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


substr returns a substring
(C++17) (public member function)
copy copies characters
(public member function of std::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)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.