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

时间:2012-08-03 09:36:21

标签: c++ arrays vector multidimensional-array

我需要向量,boolean,std:string和int,我用Google搜索和多维向量定义为:

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

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

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

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

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

5 个答案:

答案 0 :(得分:2)

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::tuple如果可以使用boost并且没有C ++ 11支持。

答案 1 :(得分:1)

您可以使用std :: tuple

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

但这不是一个多维向量。它是元组的线性向量。

答案 2 :(得分:1)

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

答案 3 :(得分:1)

包含值和向量的std::pair怎么样?即。

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

答案 4 :(得分:1)

怎么样:

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

std::vector<mytype>