我使用了 strcpy()
函数,它仅在我使用 C 字符串数组时才有效,例如:
char a[6] = "text";
char b[6] = "image";
strcpy(a,b);
但每当我使用时
string a = "text";
string b = "image";
strcpy(a,b);
我收到此错误:
functions.cpp: no matching function for call to
strcpy(std::string&, std::string&)
如何在C++中复制2个字符串数据类型的字符串?
最佳答案
您不应该使用 strcpy()
来复制 std::string
,只能将其用于 C-Style strings .
如果您想将 a
复制到 b
则只需使用 =
运算符。
string a = "text";
string b = "image";
b = a;
关于c++ - 如何在 C++ 中复制 std::string 类型的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12678819/