Joh*_*ton 2 c++ string std cstring
嗨,我有以下代码:
char msg[10000];
string mystr = "hello";
Run Code Online (Sandbox Code Playgroud)
我想将 mystr 放入 msg 中。有没有办法做到这一点?我尝试了各种方法,但不断得到:
incompatible types in assignment of 'const char*' to char [10000]'
Run Code Online (Sandbox Code Playgroud)
我试过:
msg = mystr.c_str();
Run Code Online (Sandbox Code Playgroud)
和
msg = (char[10000])mystr;
Run Code Online (Sandbox Code Playgroud)
无济于事。
你可以试试std::copy这个。就像是:
std::copy(mystr.begin(), mystr.end(), msg);
Run Code Online (Sandbox Code Playgroud)
我会避免C像C++ 中的mempcy和strcpy那样的字符串函数。