动态内存-动态数组

习题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;

猜你喜欢

转载自www.cnblogs.com/yuandonghua/p/11457113.html