跳到主要内容

std::string copy() 方法

// Const version only
constexpr size_type copy( CharT* dest, size_type count, size_type pos = 0 ) const;

将子字符串 **[ pos, pos + count )** 复制到 `dest` 指向的字符字符串。
如果请求的子字符串超出字符串末尾,或者 `count == npos`,则复制的子字符串为 **[ pos, size() )**。

生成的字符字符串**不是**以 null 结尾的。

参数

  • `dest` - 指向目标字符字符串的指针
  • count - 子字符串的长度
  • `pos` - 要包含的第一个字符的位置

返回值

复制的字符数。

复杂度

与 `count` 成线性关系 - **O(count)**。

异常

如果 pos > size(),则抛出 std::out_of_range

示例

#include <string>
#include <iostream>

int main()
{
std::string foo("quuuux");
char bar[7]{};
foo.copy(bar, sizeof bar);
std::cout << bar << '\n';
}
输出
quuuux
本文源自此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。单击“编辑此页面”可查看此文档的所有更改。
悬停查看原始许可证。

std::string copy() 方法

// Const version only
constexpr size_type copy( CharT* dest, size_type count, size_type pos = 0 ) const;

将子字符串 **[ pos, pos + count )** 复制到 `dest` 指向的字符字符串。
如果请求的子字符串超出字符串末尾,或者 `count == npos`,则复制的子字符串为 **[ pos, size() )**。

生成的字符字符串**不是**以 null 结尾的。

参数

  • `dest` - 指向目标字符字符串的指针
  • count - 子字符串的长度
  • `pos` - 要包含的第一个字符的位置

返回值

复制的字符数。

复杂度

与 `count` 成线性关系 - **O(count)**。

异常

如果 pos > size(),则抛出 std::out_of_range

示例

#include <string>
#include <iostream>

int main()
{
std::string foo("quuuux");
char bar[7]{};
foo.copy(bar, sizeof bar);
std::cout << bar << '\n';
}
输出
quuuux
本文源自此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。单击“编辑此页面”可查看此文档的所有更改。
悬停查看原始许可证。