带字符串或char的动态分配(数组)

0 c++ arrays string dynamic

这里没有问题:

int *x;
x = (int *) malloc(0 * sizeof(int));
int value = 5;
x = (int *) realloc(x,(value)*sizeof(int));
Run Code Online (Sandbox Code Playgroud)

但我不能为字符串这样做:\

我想为ay []数组执行此操作,如下所示:

y[0]="hello"
y[1]="how are you"
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

Cat*_*lus 10

std::vector<std::string> y;
y.push_back("hello");
y.push_back("how are you");
Run Code Online (Sandbox Code Playgroud)

不要使用malloc,或realloc,或free在C++中.不要char*用于互操作以外的目的.远离指针,直到你真正需要它们(数组相同).