2

问题在代码中:

template<typename... Ts>
struct TupleOfVectorsOfVectors {
  std::tuple<std::vector<std::vector<Ts>>...> tuple;

  TupleOfVectorsOfVectors () {
    //Here we already have a tuple of empty vectors.
    //Question: How can I loop through those vectors
    //and push_back into each of them an empty vector?
  }
};
4

1 回答 1

4

您可以在初始化列表中展开参数包。使用统一初始化,我认为这有效:

template<typename... Ts>
struct TupleOfVectorsOfVectors {
  std::tuple< std::vector<std::vector<Ts>> ... > tuple;

  TupleOfVectorsOfVectors ()
    : tuple { std::vector<std::vector<Ts>> { std::vector<Ts>() }... }
  { }
};

int main()
{
    TupleOfVectorsOfVectors<int, float> t;
    std::cout << std::get<0>(t.tuple).size()
              << std::get<1>(t.tuple).size(); // prints 11, as expected
}

您也可以在成员初始化程序中执行此操作(感谢@JonathanWakely):

template<typename... Ts>
struct TupleOfVectorsOfVectors {
  std::tuple< std::vector<std::vector<Ts>> ... > tuple
     { { std::vector<Ts>() }... }; // mem-initializer

  TupleOfVectorsOfVectors ()
  { }
};
于 2013-05-05T16:03:33.960 回答