0

boolean、std:string、intでベクトル化する必要があります。グーグルで多次元ベクトルを次のように定義します。

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

しかし、それは私にとって問題があります、それはただ1つのデータ型を持っています、私はペアを見つけました:

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

しかし、std :: pairには問題があり、2次元以上を定義することはできません。

質問:各次元に特定のデータ型がある多次元ベクトルを定義するにはどうすればよいですか?注:私は3次元にする必要があります。

4

5 に答える 5

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をサポートしている場合は、ブーストを使用でき、C++11をサポートしていない場合はを使用しますstd::tuple<Args...>boost::tuple

于 2012-08-03T09:41:14.410 に答える
1

std::tupleを使用できます

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

しかし、これは多次元ベクトルではありません。タプルの線形ベクトル。

于 2012-08-03T09:38:12.203 に答える
1

の唯一の問題が2次元以上の不足である場合は、 (c ++ 11)またはstd::pairのベクトルを使用できます。または、独自の構造体を作成するだけですstd::tupleboost::tuple

于 2012-08-03T09:39:09.070 に答える
1

std::pair値とベクトルの両方を含むのはどうですか?つまり

std::vector<std::pair<bool, std::vector<std::pair<std::string, str::vector<int>>>>>
于 2012-08-03T09:42:07.040 に答える
1

どうですか:

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

std::vector<mytype>

于 2012-08-03T09:46:59.123 に答える