c++ 动态内存 动态数组

动态内存-动态数组

习题12.23

//连接字符串到动态数组

char *c = new char[20]();
char a[] = "hello ";
char b[] = "world";

strcopy(c, a);
strcopy(c+strlen(a), b);

cout<<c<<endl;
// 连接string到动态数组

char *c = new char[20]();
string a = "hello ";
string b = "world";

size_t idx = 0;
for(const auto &v : a)
    c[idx++] = v;
for(const auto &v : b)
    c[idx++] = v;

cout<<v<<endl;
原文地址:https://www.cnblogs.com/yuandonghua/p/15636051.html