试图创建一系列向量(我创建的模板化向量类)
Trying to create an array of vectors (Templatized Vector class created by me)
因此,正如标题所述,我正在尝试创建一个向量,向量不是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;
}
相关文章:
- 使用 back_inserter 从结构的单个成员创建向量
- 在模板类中创建向量 C++
- C 从参数参数包创建向量或列表
- 如何为基类通用类型创建向量以使用具体类型元素
- 在类中创建向量(2D 数组)的向量 - Error:C++ 需要所有声明的类型说明符
- 是否可以在堆栈上创建向量
- 为什么"new vector<int>[5]"不创建向量数组?
- 在不创建向量的情况下检查RCPP中的非限制值
- 如何在缓冲区周围创建向量
- 使用类对象创建向量时,不会调用类构造函数
- 特征不能直接从矩阵均值创建向量
- 从类型列表创建向量元组
- 如何创建向量的向量
- 从 c 数组创建向量
- 正在使用模板类c++重新创建向量
- 如何创建向量数组
- 在 cuda 内核中创建向量
- 如何在C++中修复这个创建向量
- C++从模板迭代器创建向量
- 用new创建向量安全吗