Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 初始化字符串数组_C++ - Fatal编程技术网

C++ 初始化字符串数组

C++ 初始化字符串数组,c++,C++,我试图用std::cin初始化字符串数组,但我不能 这是我的代码: string *words[6]; for (int i=0 ; i<6;i++){ cin >> words[i]; //error } string*字[6]; for(int i=0;i>words[i];//错误 } 有人能帮我吗!!您不需要动态创建它们: string words[6]; //notice I deleted '*' here for (int i=0

我试图用std::cin初始化字符串数组,但我不能 这是我的代码:

string *words[6];

for (int i=0 ; i<6;i++){    
    cin >> words[i];    //error
}
string*字[6];
for(int i=0;i>words[i];//错误
}

有人能帮我吗!!

您不需要动态创建它们:

string words[6];    //notice I deleted '*' here
for (int i=0 ; i<6;i++){
    cin >>words[i];
}
stringwords[6];//注意我在这里删除了“*”
对于(int i=0;i>words[i];
}

您创建的是一个指针数组,其中每个指针必须先用
new
初始化,然后才能使用并删除。

您有一个指向
std::string
的指针数组,但要使代码正常工作,您需要一个
std::string
数组,请参见下面的代码:

std::string words[6][6];

for (int y=0; y<6; ++y)
  for (int x=0; x<6; ++x){
    cin >> words[x][y];
}
std::字符串字[6][6];
对于(int y=0;y字[x][y];
}

打字:
string*words[6];=>string words[6];
为什么不
std::array words;
?“字符串数组2d?”@RiadBaghbanli用户很可能混淆了
char[][]
std::string[]