向量和维度vecotr本身具有特定的数据类型

vector and dimension vecotr with itself has a specific data type

本文关键字:数据类型 vecotr 向量      更新时间:2023-10-16

我需要向量,boolean,std:string和int,我在谷歌上搜索并将多维向量定义为:

std::vector< std::vector< std::vector <std::vector<int> > > a;

但它对我来说有问题,它只有一种数据类型,我找到了一对:

std::vector<std::pair<bool,float> >  a;

但是std::pair有问题,不能定义更多的二维。

问题:如何定义多维向量,每个维度都有特定的数据类型?注意:我需要三维。

template<typename First, typename Second, typename Third>
struct triplet
{
   triplet()
   {
   }
   triplet(const First& f, const Second& s, const Third& t):
      first(f), second(s), third(t)
   {
   }
   First first;
   Second second;
   Third third;
};
template<typename First, typename Second, typename Third>
triplet make_triplet(const First& f, const Second& s, const Third& t)
{
   return triplet(f, s, t);
}

当然,如果您支持C++11,请使用std::tuple<Args...>,如果可以使用boost但没有C++11支持,则使用boost::tuple

您可以使用std::tuple

std::vector<std::tuple<bool, std::string, int>>

但这并不是一个多方面的矢量。它是元组的线性向量。

如果std::pair的唯一问题是缺少超过2个维度,则可以使用std::tuple(c++11)或boost::tuple的向量。或者只需创建自己的结构

一个同时包含值和向量的std::pair怎么样?即

std::vector<std::pair<bool, std::vector<std::pair<std::string, str::vector<int>>>>>

怎么样:

struct mytype {
    bool a;
    std::string str;
    int num;
};
std::vector<mytype>