Reputation: 1058
There is a generator which generates a tuple of 16 integers with each iteration. I want to add these tuples into a vector. While creating the vector I have to write std::vector<std::tuple<int, int, .... 16 times>>
. Is there another way to create a vector of these tuples.
Code to test for the case of tuples containing 5 integers:
#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;
}
I'm using cppiterator
Upvotes: 1
Views: 2668
Reputation: 62636
You can just fiddle the tuple type that iter::product
creates
#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;
}
Upvotes: 1
Reputation: 68571
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;
Then tuple_of<5,int>
is std::tuple<int,int,int,int,int>
.
Upvotes: 3