[英]Initializing C++ std::array with smaller arrays
如何用幾個較小的數組和非數組元素構造一個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。
如果你有可能,你可能會發現元組更容易(也更有效),直到最后創建數組。
從概念上講,你可以這樣寫:
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: 將std :: tuple轉換為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;
}
這是一個可以連接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
實現類似的功能。 看到它在這里工作 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.