簡體   English   中英

如何在C ++中組合兩個或多個任意類型的向量

[英]How to combine two or more vectors of arbitrary types in C++

我有以下代碼將兩個任意類型的向量組合成一個組合,即std::vector<std::tuple<A, B>>

template<class A, class B>
std::vector<std::tuple<A, B>> combine(const std::vector<A>& a, const std::vector<B>& b) {

    const auto combine_parts_ = [](const A& x, const B& y) {
        auto result = std::tuple_cat(std::make_tuple(x), std::make_tuple(y));
        return result;
    };

    std::vector<std::tuple<A, B>> results;

    for (const auto& x : a) {
        for (const auto& y : b) {
            results.push_back(combine_parts_(x, y));
        }
    }

    return results;
}

但是,我不清楚如何將其擴展到任意數量的類型/向量。 我不關心重復類型; 實際上可能涉及兩組或更多組相同類型。 沒關系。

一些示例用例,例如:

const auto combinations = combine(
    std::vector<int>({1,2,3})
    , std::vector<int>({1,2,3})
);
const auto combinations2 = combine(
    std::vector<int>({1,2,3})
    , std::vector<int>({1,2,3})
    , std::vector<bool>({true,false})
);
const auto combinations3 = combine(
    std::vector<int>({1,2,3})
    , std::vector<int>({1,2,3})
    , std::vector<bool>({true,false})
    , std::vector<char>({'a','b','c','d','e'})
);

首先,我想要做的是避免丑陋的嵌套for循環。 同時,我想結合一些單元測試組合用例,以便使用生成的std::tuple<...>作為測試用例。

注意,我不是在談論同類集的排列。 這與以前的問題混淆了。

我認為這可能與模板,variadics, std::tuple_cat ,在某個地方,但我不知道。

思考? 建議?

如果要計算異構向量的笛卡爾積,可以執行以下操作:

template <std::size_t N>
bool increase(const std::array<std::size_t, N>& sizes, std::array<std::size_t, N>& it)
{
    for (std::size_t i = 0; i != N; ++i) {
        const std::size_t index = N - 1 - i;
        ++it[index];
        if (it[index] >= sizes[index]) {
            it[index] = 0;
        } else {
            return true;
        }
    }
    return false;
}

template <typename F, std::size_t ... Is, std::size_t N, typename Tuple>
void apply_impl(F&& f,
                std::index_sequence<Is...>,
                const std::array<std::size_t, N>& it,
                const Tuple& tuple)
{
    f(std::get<Is>(tuple)[it[Is]]...);
}

template <typename F, typename ... Ts>
void cartesian_product_apply(F&& f, const std::vector<Ts>&... vs)
{
    constexpr std::size_t N = sizeof...(Ts);
    std::array<std::size_t, N> sizes{{vs.size()...}};
    std::array<std::size_t, N> it{};

    do {
        apply_impl(f, std::index_sequence_for<Ts...>(), it, std::tie(vs...));
    } while (increase(sizes, it));
}

最后:

template <typename ... Ts>
std::vector<std::tuple<Ts...>> cartesian_product(const std::vector<Ts>&... vs)
{
    std::vector<std::tuple<Ts...>> res;

    cartesian_product_apply([&res](const auto&... args) { res.emplace_back(args...); },
                            vs...);
    return res;
}

使用類似於:

std::vector<int> v1 = {1, 2, 3};
std::vector<std::string> v2 = {" A "," B "};
std::vector<int> v3 = {4, 5};

const auto res = cartesian_product(v1, v2, v3);
for (const auto& t : res) {
    // ...
}

演示

稍微適應,並禁用警告:

struct CartesianProduct {

    template<typename... Tys>
    std::vector<std::tuple<Tys...>> operator()(const std::vector<Tys>&... vectors) {
        std::vector<std::tuple<Tys...>> results;
        apply([&results](const auto&... args) {
            results.emplace_back(args...); }, vectors...);
        return results;
    }

private:

    template<std::size_t N>
    bool __increase(const std::array<std::size_t, N>& sizes, std::array<std::size_t, N>& it) {
        for (std::size_t i = 0; i < N; ++i) {
            const std::size_t index = N - 1 - i;
            ++it[index];
            if (it[index] >= sizes[index]) {
                it[index] = 0;
            }
            else {
                return true;
            }
        }
        return false;
    }

    template<typename Cb, std::size_t... Is, std::size_t N, typename Tup>
    void __apply_impl(Cb&& cb, std::index_sequence<Is...>
        , const std::array<std::size_t, N>& it, const Tup& tup) {
        cb(std::get<Is>(tup)[it[Is]]...);
    }

    template <typename Cb, typename... Tys>
    void apply(Cb&& cb, const std::vector<Tys>&... vectors) {
        constexpr std::size_t N = sizeof...(Tys);
        std::array<std::size_t, N> sizes{ {vectors.size()...} };
#pragma warning (disable: 4834)
        // TODO: TBD: warning C4834: discarding return value of function with 'nodiscard' attribute...
        std::array<std::size_t, N> it{ {(vectors.size(), 0u)...} };
#pragma warning (default: 4834)

        do {
            __apply_impl(cb, std::index_sequence_for<Tys...>(), it, std::tie(vectors...));
        } while (__increase(sizes, it));
    }
};

記得包括tuplevector ,以及array

使用簡單:

CartesianProduct prod;
// ...
const auto product_ = prod(ints_, doubles_, bools_);
CHECK(std::tuple_size<decltype(product_ )::value_type>::value == 3);

雖然並不完全肯定警告的全部內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM