将字符串添加到未知大小的数组C++ 我在C++中遇到了一些问题。 我想让用户以字符串的形式给程序一个输入,并一直这样做直到用户满意为止。我的输入工作正常,但当我想将字符串存储到数组中时,我遇到了一些问题。我必须为我的数组定义一个大小?有没有一种方法可以根据输入将输入存储在2或3个不同的数组中,我用一些字符串的if语句对输入进行排序,然后打印出来? 我的代码现在看起来像这样 string firstarray[10]; string secarray[10]; //The cin stuff here and reading strings from user-input if(MyCondition1){ for(int x = 0; x<=9;x++){ firstarray[x] = name; } if(MyCondition2){ for(int x = 0; x<=9;x++){ secarray[x] = name; }

将字符串添加到未知大小的数组C++ 我在C++中遇到了一些问题。 我想让用户以字符串的形式给程序一个输入,并一直这样做直到用户满意为止。我的输入工作正常,但当我想将字符串存储到数组中时,我遇到了一些问题。我必须为我的数组定义一个大小?有没有一种方法可以根据输入将输入存储在2或3个不同的数组中,我用一些字符串的if语句对输入进行排序,然后打印出来? 我的代码现在看起来像这样 string firstarray[10]; string secarray[10]; //The cin stuff here and reading strings from user-input if(MyCondition1){ for(int x = 0; x<=9;x++){ firstarray[x] = name; } if(MyCondition2){ for(int x = 0; x<=9;x++){ secarray[x] = name; },c++,arrays,string,C++,Arrays,String,?您正在寻找一个合适的人选。或者更好的是,一个允许您通过其位置访问图元的 它们都可以动态扩展 using namespace std; // looks like this: vector<string> firstvector; firstvector.push_back(somestring); // appends somestring to the end of the vector cout << firstvector[someindex]; // ge

您正在寻找一个合适的人选。或者更好的是,一个允许您通过其位置访问图元的

它们都可以动态扩展

using namespace std;

// looks like this:
vector<string> firstvector;

firstvector.push_back(somestring); // appends somestring to the end of the vector

cout << firstvector[someindex]; // gets the string at position someindex
cout << firstvector.back(); // gets the last element
然后要输出它,您可以简单地循环每个类别和向量元素

for(int i = 0; i < categoryMap.size(); i++)
  for(int j = 0; j < categoryMap[i].size(); j++)
    cout << categoryMap[i][j];
你在找一份工作。或者更好的是,一个允许您通过其位置访问图元的

它们都可以动态扩展

using namespace std;

// looks like this:
vector<string> firstvector;

firstvector.push_back(somestring); // appends somestring to the end of the vector

cout << firstvector[someindex]; // gets the string at position someindex
cout << firstvector.back(); // gets the last element
然后要输出它,您可以简单地循环每个类别和向量元素

for(int i = 0; i < categoryMap.size(); i++)
  for(int j = 0; j < categoryMap[i].size(); j++)
    cout << categoryMap[i][j];

您是否考虑过使用std::vector>?

您是否考虑过使用std::vector>?

您尝试使用多个阵列解决的问题是什么?矢量阵列;摆脱大小限制。你的意思是std::string,对吗?我的意思是,我没有看到任何地方使用的命名空间……他用了没有STD的字符串:在这个问题上。没有使用STD::,对C++有一点新的地方。你用多个数组来解决什么问题?矢量阵列;摆脱大小限制。你的意思是std::string,对吗?我的意思是,我没有看到任何地方使用的命名空间…他使用了没有STD的字符串:在这个问题上。没有使用STD::,有点新的C++。我在一些谷歌的结果中遇到了。但我认为这是过分的,我想我错了。有人能链接到how-to吗?@Handsken这一点也不过分,向量比数组更容易使用。看看Martin的例子,它应该会让你走上正轨。我在谷歌的一些搜索结果中遇到过它。但我认为这是过分的,我想我错了。有人能链接到how-to吗?@Handsken这一点也不过分,向量比数组更容易使用。看看Martin的例子,它会让你走上正确的道路。