2

std::tuple を指定すると、次のようになります。

std::tuple<int, float, char>

次のようなタイプを生成したいと思います。

std::tuple<std::vector<int>, std::vector<float>, std::vector<char>>

ご覧のとおり、元の型のベクトルのタプルです。標準的なシナリオは次のとおりです。

typedef std::tuple<int, float, char>    Struct;          // scenario 1
typedef std::vector<Struct>             ArrayOfStructs;  // scenario 2
typedef HereIsTheQuestion<Struct>::Type StructOfArrays;  // scenario 3

シナリオ 1 は、次のようにアクセスすることを意図しています。

Struct x = ...; // single tuple
std::get<0>(x) = 11;
// etc.

シナリオ 2 は、次のようにアクセスすることを意図しています。

ArrayOfStructs xs = ...; // array of tuples
for (size_t i=0; i<xs.size(); ++i) {
    std::get<0>(xs[i]) = 11;
    // etc.
}

シナリオ 3 は、次のようにアクセスすることを意図しています。

StructsOfArrays xs = ...; // single tuple of arrays
size_t n = std::get<0>(xs).size(); // first tuple array size
for (size_t i=0; i<n; ++i) {
    std::get<0>(xs)[i] = 11;
    // etc.
}

HereIsTheQuestion::Type を元の Struct 型の配列のタプルに似せるには、どのように記述する必要がありますか?

ありがとう、M.

4

2 に答える 2

4

HereIsTheQuestion実装方法は次のとおりです。

template<typename T>       //primary template
struct HereIsTheQuestion;  //leave it undefined

template<typename ...T>
struct HereIsTheQuestion<std::tuple<T...>>  //partial specialization
{
    using Type = std::tuple<std::vector<T>...>;
};

HereIsTheQuestion<std::tuple<int, float, char>>::Type

std::tuple<std::vector<int>,std::vector<float>, std::vector<char>>

それが役立つことを願っています。

于 2013-06-23T18:26:45.550 に答える