查看std :: vector <std :: tuple <ts ... >>中的所有T

时间:2019-04-21 21:40:07

标签: c++ vector tuples c++14 c++17

想象一下,我有一个类似于std::vector<std::tuple<Ts...>>的容器。

由此,我想对所有view都使用“ T”(非复制),这样我就可以在该视图上进行操作,就好像它是标准容器

所以我想拥有什么

using tuple_vector = std::vector<std::tuple<int,float,double>>;
tuple_vector tuple_vec = {{1, 4.f, 8.},
                          {2, 5.f, 9.},
                          {3, 6.f, 10.},
                          {4, 7.f, 11.}}

auto int_view = view<int>(tuple_vec);
^^^
type should be some kind of non-owning reference 

// what I would like to do
int_view[0] = 10; // modify
assert(int_view[0] == std::get<int>(tuple_vec[0])); // modification should modify tuple_vec as well

我尝试过std::transform,但是后来我得到了所有int拥有副本

std::vector<int> int_vec(tuple_vec.size());
std::transform(tuple_vec.begin(), tuple_vec.end(), int_vec.begin(), [&](const auto& elem) {
    return std::get<int>(elem);
});

我不确定这是否完全可能,但是如果可以,我将不胜感激。

2 个答案:

答案 0 :(得分:3)

好吧,如果您使用Eric Niebler's ranges-v3 library(正在被采纳为标准,现在有Ranges TS),则可以运用直觉来使用类似std::transform的东西,但视图:

#include <range/v3/view/transform.hpp>

// ... etc. ...

auto int_view = tuple_vector | ranges::view::transform(
    [](auto& t)->auto&{ return std::get<int>(t);} );

请参见action(Coliru),甚至还要修改其中一个元素。

注意:如果我们移除-> decltype(auto),则该视图将无法修改;更改归因于@deduplicator对this question的答复。

答案 1 :(得分:3)

您可以创建std::reference_wrapper的向量:

template <typename T, typename ContainerOfTuples>
auto make_refs_to(ContainerOfTuples& tuples) {
    using RefType = std::reference_wrapper<T>;

    std::vector<RefType> refs;
    refs.reserve(std::size(tuples));
    std::transform(std::begin(tuples), std::end(tuples), std::back_inserter(refs),
                   [](auto& tup) -> RefType { return {std::get<T>(tup)}; });
    return refs;
}

auto double_view = make_refs_to<double>(tuple_vec);
double_view[1].get() += 3.14;  // Caveat: must access through .get().

Live Example


更进一步...这是我们如何获得对许多类型的引用的方法:

namespace detail {

// When many types are asked for, return a tuple of references.
template <typename... T> struct RefTypeImpl {
    using type = std::tuple<std::reference_wrapper<T>...>;
};

// When a single type is asked for, return a single reference.
template <typename T> struct RefTypeImpl<T> {
    using type = std::reference_wrapper<T>;
};

// When two types are asked for, return a pair for more convenient access.
template <typename T, typename U> struct RefTypeImpl<T, U> {
    using type = std::pair<std::reference_wrapper<T>, std::reference_wrapper<U>>;
};

}  // namespace detail

template <typename... Ts, typename ContainerOfTuples>
auto make_refs_to(ContainerOfTuples& tuples) {
    using RefType = typename detail::RefTypeImpl<Ts...>::type;

    std::vector<RefType> refs;
    refs.reserve(std::size(tuples));
    std::transform(std::begin(tuples), std::end(tuples), std::back_inserter(refs),
                   [](auto& tup) -> RefType { return {std::get<Ts>(tup)...}; });
    return refs;
}

auto int_float_view = make_refs_to<int, float>(tuple_vec);
std::cout << (int_float_view[2].first.get() == 3) << '\n';

Live Example