使用较小的数组初始化C ++ std :: array

时间:2015-11-27 10:24:15

标签: c++ arrays c++11 stl

如何用几个较小的数组加上非数组元素构造一个std :: array?

即:

std::array<std::string, 2> strings = { "a", "b" };
std::array<std::string, 2> more_strings = { "c", "d" };
std::string another_string = "e";

std::array<std::string, 5> result = {strings, more_strings, another_string};

同样的问题适用于使用较小的std :: initializer_lists初始化std :: initializer_list。

2 个答案:

答案 0 :(得分:1)

如果您可以使用,您可能会发现元组更容易(也更有效),直到最终创建数组。

从概念上讲,你可以这样写:

    using namespace std;
    auto strings = make_tuple( "a"s, "b"s );
    auto more_strings = make_tuple( "c"s, "d"s);
    auto another_string = make_tuple("e"s);
    auto result = to_array(tuple_cat(strings, more_strings, another_string));

当然,您需要为to_array实现一个非常简单的实现。完整的代码如下,在这个答案中,Luc Danton可以获得信用: Convert std::tuple to std::array C++11

完整代码供参考:

#include <iostream>
#include <string>
#include <tuple>
#include <array>


template<int... Indices>
struct indices {
    using next = indices<Indices..., sizeof...(Indices)>;
};

template<int Size>
struct build_indices {
    using type = typename build_indices<Size - 1>::type::next;
};

template<>
struct build_indices<0> {
    using type = indices<>;
};

template<typename T>
using Bare = typename std::remove_cv<typename std::remove_reference<T>::type>::type;

template<typename Tuple>
constexpr
typename build_indices<std::tuple_size<Bare<Tuple>>::value>::type
make_indices()
{ return {}; }

template<typename Tuple, int... Indices>
std::array<
typename std::tuple_element<0, Bare<Tuple>>::type,
std::tuple_size<Bare<Tuple>>::value
>
to_array(Tuple&& tuple, indices<Indices...>)
{
    using std::get;
    return {{ get<Indices>(std::forward<Tuple>(tuple))... }};
}

template<typename Tuple>
auto to_array(Tuple&& tuple)
-> decltype( to_array(std::declval<Tuple>(), make_indices<Tuple>()) )
{
    return to_array(std::forward<Tuple>(tuple), make_indices<Tuple>());
}


auto main() -> int
{
    using namespace std;

    auto strings = make_tuple( "a"s, "b"s );
    auto more_strings = make_tuple( "c"s, "d"s);
    auto another_string = make_tuple("e"s);

    auto result = to_array(tuple_cat(strings, more_strings, another_string));
    for (const auto& e : result)
    {
        cout << e << endl;
    }
    return 0;
}

答案 1 :(得分:0)

这是一个可以加入std::array的函数。

template<typename T>
auto wrap_value(T&& value)
{
    return std::tuple<T&&>(std::forward<T>(value));
}

template<typename T, std::size_t N>
std::array<T, N>& wrap_value(std::array<T, N>& value)
{
    return value;
}

template<typename T, std::size_t N>
std::array<T, N> const& wrap_value(std::array<T, N> const& value)
{
    return value;
}

template<typename T, std::size_t N>
std::array<T, N>&& wrap_value(std::array<T, N>&& value)
{
    return std::move(value);
}

template<std::size_t... Is, typename... Ts>
std::array<std::common_type_t<Ts...>, sizeof...(Is)>
join_arrays_impl(std::index_sequence<Is...>, std::tuple<Ts...>&& parts)
{
    return {std::get<Is>(std::move(parts))...};
}

template<typename... Ts>
auto join_arrays(Ts&&... parts)
{
    auto wrapped_parts = std::tuple_cat((wrap_value)(std::forward<Ts>(parts))...);
    constexpr auto size = std::tuple_size<decltype(wrapped_parts)>::value;
    std::make_index_sequence<size> seq;
    return (join_arrays_impl)(seq, std::move(wrapped_parts));
}

我认为你不能为std::initializer_list实现类似的功能。 See it working here