如何在C ++中复制std :: string类型的字符串? [英] How to copy a string of std::string type in C++?

查看:634
本文介绍了如何在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);

我收到此错误:


函数。 cpp:没有匹配函数可调用 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类型的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆