std::basic_string_view<CharT,Traits>::copy
來自 cppreference.com
< cpp | string | basic string view
size_type copy( CharT* dest, size_type count, size_type pos = 0 ) const; |
(C++17 起) (C++20 起為 constexpr) |
|
將子字串 [
pos,
pos + rcount)
複製到 dest 指向的字元陣列,其中 rcount
是 count 和 size() - pos 中較小的一個。
等價於 Traits::copy(dest, data() + pos, rcount)。
目錄 |
[編輯] 引數
dest | - | 指向目標字元字串的指標 |
count | - | 請求的子字串長度 |
pos | - | 首字元的位置 |
[編輯] 返回值
複製的字元數。
[編輯] 異常
如果 pos > size() 則丟擲 std::out_of_range。
[編輯] 複雜度
與 rcount
成線性關係。
[編輯] 示例
執行此程式碼
#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)
[編輯] 參閱
返回子字串 (public member function) | |
複製字元 ( std::basic_string<CharT,Traits,Allocator> 的公共成員函式) | |
(C++11) |
將一個範圍的元素複製到一個新位置 (函式模板) |
將一個緩衝區複製到另一個緩衝區 (函式) |