std::basic_string<CharT,Traits,Allocator>::copy
來自 cppreference.com
< cpp | string | basic_string
size_type copy( CharT* dest, size_type count, size_type pos = 0 ) const; |
(C++20 起為 constexpr) | |
將子字串 [
pos,
pos + count)
複製到 dest 指向的字元字串。如果請求的子字串超出了字串的末尾,或者如果 count == npos,則複製的子字串為 [
pos,
size()
)
。
生成的字元字串不是以 null 結尾的。
目錄 |
[編輯] 引數
dest | - | 指向目標字元字串的指標 |
count | - | 子字串的長度 |
pos | - | 要包含的第一個字元的位置 |
[編輯] 返回值
複製的字元數。
[編輯] 異常
如果 pos > size(),則丟擲 std::out_of_range。
如果由於任何原因丟擲異常,此函式無效果(強異常安全保證)。
[編輯] 複雜度
關於 count 的線性複雜度。
[編輯] 示例
執行此程式碼
#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 }
輸出
WIN
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 847 | C++98 | 沒有異常安全保證 | 添加了強異常安全保證 |
[編輯] 另請參閱
返回子字串 (public member function) | |
複製字元 ( std::basic_string_view<CharT,Traits> 的 public 成員函式) | |
(C++11) |
將一個範圍的元素複製到一個新位置 (函式模板) |
將一個緩衝區複製到另一個緩衝區 (函式) |