Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.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++程序,其中你必须输入一组名称。您可以输入的名称数量没有限制。输入完姓名后,只需键入“1”即可退出。这是我的密码: #include <iostream> #include <string> using namespace std; int main() { string name; int nofPeople = 0; cout<<"Enter a name or 1 to quit:\n"; cin>>name; while(name != "1") { nofPeople = nofPeople + 1; cout<<"Enter another name or 1 to quit:\n"; cin>>name; } } #包括 #包括 使用名称空间std; int main() { 字符串名; int nofPeople=0; coutname; 而(名称!=“1”) { nofPeople=nofPeople+1; coutname; } }_C++_Arrays_String_Input_Sentinel - Fatal编程技术网

如何创建一个由以前输入的元素组成的数组? 我编写了一个前哨控制的C++程序,其中你必须输入一组名称。您可以输入的名称数量没有限制。输入完姓名后,只需键入“1”即可退出。这是我的密码: #include <iostream> #include <string> using namespace std; int main() { string name; int nofPeople = 0; cout<<"Enter a name or 1 to quit:\n"; cin>>name; while(name != "1") { nofPeople = nofPeople + 1; cout<<"Enter another name or 1 to quit:\n"; cin>>name; } } #包括 #包括 使用名称空间std; int main() { 字符串名; int nofPeople=0; coutname; 而(名称!=“1”) { nofPeople=nofPeople+1; coutname; } }

如何创建一个由以前输入的元素组成的数组? 我编写了一个前哨控制的C++程序,其中你必须输入一组名称。您可以输入的名称数量没有限制。输入完姓名后,只需键入“1”即可退出。这是我的密码: #include <iostream> #include <string> using namespace std; int main() { string name; int nofPeople = 0; cout<<"Enter a name or 1 to quit:\n"; cin>>name; while(name != "1") { nofPeople = nofPeople + 1; cout<<"Enter another name or 1 to quit:\n"; cin>>name; } } #包括 #包括 使用名称空间std; int main() { 字符串名; int nofPeople=0; coutname; 而(名称!=“1”) { nofPeople=nofPeople+1; coutname; } },c++,arrays,string,input,sentinel,C++,Arrays,String,Input,Sentinel,现在我想创建一个长度等于'nofPeople'的数组,并且我希望该数组的元素是我已经输入的名称。如何做到这一点?实现这一点的标准方法是创建一个std::vector,在接收字符串时向其中添加字符串,最后(收集完所有字符串后)将向量转换为数组 但是,除非你没有提到其他奇怪的要求,我强烈建议您忘记使用数组,在整个程序中继续使用向量。您可以使用std::vector及其push_back方法在用户输入向量时将名称添加到向量中。您可能还希望在某些情况下防止流错误和/或eof邪恶的人在EOF标记中而不是“

现在我想创建一个长度等于'nofPeople'的数组,并且我希望该数组的元素是我已经输入的名称。如何做到这一点?

实现这一点的标准方法是创建一个
std::vector
,在接收字符串时向其中添加字符串,最后(收集完所有字符串后)将向量转换为数组


但是,除非你没有提到其他奇怪的要求,我强烈建议您忘记使用数组,在整个程序中继续使用向量。

您可以使用
std::vector
及其
push_back
方法在用户输入向量时将名称添加到向量中。

您可能还希望在某些情况下防止流错误和/或eof邪恶的人在EOF标记中而不是“1”。我对C++编程很陌生,所以我不熟悉STD::vector。你能给我举一个我如何使用它的例子吗?非常感谢。