如何在C++中创建元组向量?
How to create a vector of tuples in C++?
有一个生成器,每次迭代都会生成一个包含 16 个整数的元组。我想将这些元组添加到向量中。在创建向量时,我必须写std::vector<std::tuple<int, int, .... 16 times>>
.有没有另一种方法可以创建这些元组的向量。
用于测试包含 5 个整数的元组大小写的代码:
#include "cppitertools/itertools.hpp"
#include <iostream>
#include <tuple>
#include <vector>
int main()
{
std::vector<int> v1{0, 1};
std::vector<std::tuple<int, int, int, int, int>> states;
for (auto&& i : iter::product<5>(v1))
{
states.push_back(i);
}
auto size = states.size();
std::cout << size << std::endl;
}
我正在使用 cppiterator
template<size_t Remaining, typename Type,typename ... Args>
struct make_tuple_n_impl{
using type=typename make_tuple_n_impl<Remaining-1,Type,Type,Args...>::type;
};
template<typename T,typename ... Args>
struct make_tuple_n_impl<0,T,Args...>{
using type=std::tuple<Args...>;
};
template<size_t Count,typename Type>
using tuple_of=typename make_tuple_n_impl<Count,Type>::type;
那么tuple_of<5,int>
std::tuple<int,int,int,int,int>
.
您可以摆弄iter::product
创建的元组类型
#include "cppitertools/itertools.hpp"
#include <iostream>
#include <tuple>
#include <vector>
template <typename>
struct remove_tuple_reference;
template <typename... Ts>
struct remove_tuple_reference<std::tuple<Ts...>> {
using type = std::tuple<std::remove_reference_t<Ts>...>;
};
template <typename T>
using remove_tuple_reference_t = typename remove_tuple_reference<T>::type;
int main()
{
std::vector<int> v1{0, 1};
using tup = remove_tuple_reference_t<decltype(iter::product<5>(v1).begin())::value_type>;
std::vector<tup> states;
for (auto&& i : iter::product<5>(v1))
{
states.push_back(i);
}
auto size = states.size();
std::cout << size << std::endl;
}
相关文章:
- 将元组的向量转换/构造为堆
- 将元组的向量构造成堆
- 元组由 Swig 生成的 Python 包装器返回,用于C++向量
- 我正在寻找一种优雅的方式来从元组向量创建tuple_element向量
- 如何在元组初始化向量中删除样板?
- c++:交换向量中所有元组的第一个和第二个元素
- 如何在C++中创建元组向量?
- 返回两个向量 – 使用引用还是元组?
- 将可变参数模板包装到元组向量
- C++ 从模板参数创建元组向量
- 随机访问元组向量中的元组值
- 如何构造元组向量并像配对一样对它们进行排序?
- 通过移动从两个向量创建元组向量
- 使用GCC v4.8在Ubuntu 14.04上定义C 11中元组向量时的编译错误
- 在元组向量中查找特定的元组元素
- 如何使用 C++11 可变参数模板来定义由向量元组支持的元组向量
- c++中的元组向量
- 查找boost元组向量中元素的位置
- 自定义对元组向量进行排序
- 自定义排序元组向量时出错