试图创建一系列向量(我创建的模板化向量类)

Trying to create an array of vectors (Templatized Vector class created by me)

本文关键字:创建 向量 一系列      更新时间:2023-10-16

因此,正如标题所述,我正在尝试创建一个向量,向量不是stl,我已经制作了自己的最小班级nate vector.h,这基本上是一个动态可以容纳任何类型的数组(模板化(

我正在从文件中读取大量数据,并将每行分为有意义的属性,然后将其存储到我制作的向量中。

Vector<string> myDateVector;
Vector<string> myTimeVector;
Vector<float> mySolarRadiationVector;
Vector<float> myWindSpeedVector;
Vector<float> myAirTempVector;

现在,我想制作这些向量的数组,其中数组的每个索引都包含所有这些向量中数据的指针,例如Array [1]中的MyDateVector.get(1(;时间(1(求解(1(风速(1(airtemp(1(。

现在对C 的新事物以及研究STL向量以及与我的工作无关的事物非常遗憾,这使我更加困惑,因此我发现需要发布此信息。我真的希望这不是重复的!

如果我正确理解您的问题,您想将所有这些向量存储在一个数组中。

由于几种原因,这是不可能的。数组的所有元素必须具有相同的类型,而向量则具有不同的类型。

如果您真的想做这件事,那么您有两种(或更多,取决于您的幻想(。

  • 创建向量的元组。最简单的变体,但是如果您选择的话,您必须阅读有关元素的一两篇文章。

  • Vector<float>移至Vector<std::string>。使用std::to_string,卢克!然后,只需使用Vector<std::string> arr[5] = { /* here's your vectors of strings*/} ;即可。

我必须为我的标记道歉,因为移动应用程序的使用非常不舒服。所以我愿意。

自己写一个容器

struct MyData
{
  std::tuple<string &, string &, float &, float &, float &> operator[](size_t row)
  {
    return std::tie(myDateVector[row], 
                    myTimeVector[row],
                    mySolarRadiationVector[row],
                    myWindSpeedVector[row],
                    myAirTempVector[row]);
  }
  string & operator()(size_t row, size_t col) 
  { 
    switch (col) 
    {
      case 0: return myDateVector[row];
      case 1: return myTimeVector[row];
    }
  }
  float & operator()(size_t row, size_t col)
  { 
    switch (col) 
    {
      case 2: return mySolarRadiationVector[row];
      case 3: return myWindSpeedVector[row];
      case 4: return myAirTempVector[row];
    }
  }
// and other useful members
private:
  Vector<string> myDateVector;
  Vector<string> myTimeVector;
  Vector<float> mySolarRadiationVector;
  Vector<float> myWindSpeedVector;
  Vector<float> myAirTempVector;
}