在数组中存储 n 个向量

storing n numbers of vectors in array

本文关键字:向量 存储 数组      更新时间:2023-10-16

我有n个向量,这些向量是根据用户输入生成的。我想将它们存储在数组或向量中。

我来自php,

在php中,您可以将数组存储在另一个数组中。如何在 c++ 中实现这一点。通过在数组或向量内存储 n 个向量。

这在某种程度上就是在 PHP 中实现它的方式,假设 PHP 有向量

    for (int i = 0 ; i< userInput ; i++)
     {
         arrayOfVectors[] =  vector<string> students_1;
      }

最简单的方法是使用向量向量:

std::vector<std::vector<string>> data (userInput);

这将创建一个包含 userInputs 个字符串向量的向量。如何使用它取决于你的要求,这些问题并不清楚,至少对于不熟悉 PHP 的人来说是不清楚的。

使用这个:

std::vector<std::vector<string>> vectors(userInput);
vectors.push_back(students_1);
vectors.push_back(students_2);
vectors.push_back(students_3);
// an so on

请注意,boost 具有多维数组

像这样:

#include<vector>
#include<string>
typedef vector<std::string> student_vector;
std::vector<student_vector> arrayOfVectors;
for(..)
{
  student_vector student;
  student.push_back("SomeValue");
  student.push_back("SomeValue2");
  arrayOfVectors.push_back(student);
}