将一个std::数组拆分为一个较小大小的std::array元组
Spliting a std::array into a tuple of smaller sized std::array
我正试图将std::array<T, N>
拆分为较小数组的元组,如std::tuple<std::array<T, N1>, std::array<T, N2>, ...>
,其中N1 + N2 + ... = N.
namespace detail {
// Summation of the given values
template <class T>
constexpr T sum(const T& x) { return x; }
template <class T, class ...Args>
constexpr auto sum(const T& x, Args&&... args)
{ return x + sum(std::forward<Args>(args)...); }
}
template <class T, std::size_t... Ns>
constexpr
std::tuple<std::array<T, Ns>...>
f(const std::array<T, detail::sum(Ns...)>& x)
{
// How do I implement this function?
}
int main()
{
constexpr std::array<Foo, 5> arr = { ... };
constexpr auto t = f<Foo, 2,3>(arr);
}
事实上,我已经实现了f
,但它基于一个循环,该循环只是创建一个空数组并复制给定数组的元素,但如果t不是default_constructible
,它就不起作用。
我试着使用std::integer_sequence
和std::make_index_sequence
,但我想我完全迷失了方向。
有人能帮我实现这个功能吗?
写入
template<class T,size_t...Is,size_t N>
std::array<T,sizeof...(Is)>
extract(std::array<T,N>const&,std::index_sequence<Is...>){
return {{arr[Is]...}};
}
现在我们只需要将{1,2,3}
粗略地转换为{{0},{1,2},{3,4,5}}
,所有内容都是C++索引序列(所以语法)。
将{3,4,0}
映射到{0,1,2}
——子数组的索引计数。然后将CCD_ 12 x 1
映射到CCD_。这给了我们子数组中的索引,我们将其提供给extract
,bob是你的叔叔。
template<size_t n, size_t...counts>
constexpr auto
foo( std::index_sequence<counts...> )
-> offset_seq<
sum_n<n>(counts...),
std::make_index_sequence<get_n<n,counts...> >
>{ return {}; }
例如{3,4,0}
x 1
到{3,4,5,6}
部分。
相关文章:
- 使用一个考虑到std::map中键值的滚动或换行的键
- 使用std::transform将一个范围的元素添加到另一个范围中
- 我想访问std::unique_ptr中的一个特定元素
- 在一个模板函数中,若输入的类型是enum类,我该如何使用std::underlying_type
- 库函数需要一个 std::function<void(void)>,如何传入类函数?
- 使用std::mt19937从字符串中返回一个随机单词
- 分离一个静态常量 std::thread?
- 使用运算符 [] 引用 std::vector 上最后一个元素时出现问题<>
- 为 std::variant 提供一个运算符 ==
- 返回一个带有 std::move 的对象并链接函数
- 为什么 C++ std::unordered_map 从 emplace/ 找到返回一个迭代器?
- std::find,返回所有找到的值的替代方法,而不仅仅是存在重复的向量的第一个值
- 如何创建一个类,以便向量工作 std::vector<MyClass<int>> v{ 1,2,3 };
- C++ STD 函数运算符:有没有一种方法可以通过函数将一个向量映射到另一个向量上?
- 将 std::array 移动到另一个 std::array
- 为什么当我为 for(auto& it : myUnorderedMap) {... = std::move(it.second)} 时,我会得到一个 const 引用?
- 为什么在 std::map 上移动无法将元素从一个映射移动到另一个映射
- 使用 std::transform 将向量向量 (a) 添加到另一个向量向量 (b)
- 创建一个没有复制构造函数的类的 std::vector 的 std::vector
- Std::一个未知大小的数组作为类成员