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;
posted @ 2021-12-02 23:41  下夕阳  阅读(97)  评论(0编辑  收藏  举报