How to copy a string of std::string type in C++?(如何在 C++ 中复制 std::string 类型的字符串?)
问题描述
我使用了 strcpy() 函数,它仅在我使用 C 字符串数组时才有效,例如:
I used the strcpy() function and it only works if I use C-string arrays like:
char a[6] = "text";
char b[6] = "image";
strcpy(a,b);
但每当我使用时
string a = "text";
string b = "image";
strcpy(a,b);
我收到此错误:
functions.cpp: 没有匹配的函数调用 strcpy(std::string&, std::string&)
functions.cpp: no matching function for call to
strcpy(std::string&, std::string&)
如何在C++中复制2个字符串数据类型的字符串?
How to copy 2 strings of string data type in C++?
推荐答案
你不应该使用 strcpy() 来复制 std::string,只能使用它对于 C 样式字符串.
You shouldn't use strcpy() to copy a std::string, only use it for C-Style strings.
如果您想将 a 复制到 b 则只需使用 = 运算符.
If you want to copy a to b then just use the = operator.
string a = "text";
string b = "image";
b = a;
这篇关于如何在 C++ 中复制 std::string 类型的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 C++ 中复制 std::string 类型的字符串?
基础教程推荐
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- CString 到 char* 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
