C++ 字符串到固定大小的 char 数组可能吗?

标签 c++ string std cstring

您好,我有以下代码:

char msg[10000];
string mystr = "hello";

我想将 mystr 放入 msg 中。有没有办法做到这一点?我尝试了各种方法,但不断得到:

incompatible types in assignment of 'const char*' to char [10000]'

我试过:

msg = mystr.c_str();

msg = (char[10000])mystr;

没用。

最佳答案

你可以试试std::copy为了这。像这样的东西:

std::copy(mystr.begin(), mystr.end(), msg);

我会避免 C 字符串函数,例如 C++ 中的 mempcystrcpy

关于C++ 字符串到固定大小的 char 数组可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11625904/

相关文章:

swift - 从另一个 TableView 添加行 - Swift

c# - 如何防止两个线程复制方法和变量

c++ - 字典序、字符串、c++

c++ - std::shared_ptr 是如何多态的?

c++ - find() 无法识别我的自定义迭代器

c++ - 用户在 map 中选择项目

c++ - 如何从模板中获取容器的类型(例如 int、double 等)? C++

java - 字符串基本查询

c++ - 以 API 向后兼容的方式添加新的命名空间

c++ - std::remove 指定元素的 vector