Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/string/basic string view/copy"

From cppreference.com
m ({{range}}, {{c}}, .)
m (Synopsis: +constexpr since)
 
Line 1: Line 1:
 
{{cpp/string/basic_string_view/title|copy}}
 
{{cpp/string/basic_string_view/title|copy}}
 
{{cpp/string/basic_string_view/navbar}}
 
{{cpp/string/basic_string_view/navbar}}
{{dcl begin}}
+
{{ddcl|since=c++17|notes={{mark constexpr since c++20}}|1=
{{dcl rev multi|since1=c++17|dcl1=
+
 
size_type copy( CharT* dest, size_type count, size_type pos = 0 ) const;
 
size_type copy( CharT* dest, size_type count, size_type pos = 0 ) const;
|since2=c++20|dcl2=
 
constexpr size_type copy( CharT* dest, size_type count,
 
                          size_type pos = 0 ) const;
 
 
}}
 
}}
{{dcl end}}
 
  
 
Copies the substring {{range|pos|pos + rcount}} to the character array pointed to by {{c|dest}}, where {{tt|rcount}} is the smaller of {{c|count}} and {{c|size() - pos}}.
 
Copies the substring {{range|pos|pos + rcount}} to the character array pointed to by {{c|dest}}, where {{tt|rcount}} is the smaller of {{c|count}} and {{c|size() - pos}}.

Latest revision as of 15:09, 29 April 2024

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

Copies the substring [pospos + 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).

Contents

[edit] Parameters

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

[edit] Return value

Number of characters copied.

[edit] Exceptions

std::out_of_range if pos > size().

[edit] Complexity

Linear in rcount.

[edit] Example

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

[edit] See also

returns a substring
(public member function) [edit]
copies characters
(public member function of std::basic_string<CharT,Traits,Allocator>) [edit]
copies a range of elements to a new location
(function template) [edit]
copies one buffer to another
(function) [edit]