How can I convert std::vector to std::tuple ? I have
class T { };
int cnt = 3;
vector<T*> tv;
for (int i = 0; i < cnt; ++i) {
tv.push_back(new T());
}
I want to get
auto tp = std::tie(*tv[0], *tv[1], *tv[2]);
How can I get this tp ? If cnt is big enough, I can't write this tp manually.
std::vector<
ConvConnection<
decltype(inputLayer),
decltype(*C1[0]),
decltype(*Conn1Opt[0]),
RandomInitialization<arma::mat>,
arma::mat
>* > Conn1(6);
for (size_t i = 0; i < 6; ++i) {
Conn1.push_back(new ConvConnection<
decltype(inputLayer),
decltype(*C1[0]),
decltype(*Conn1Opt[0]),
RandomInitialization<arma::mat>,
arma::mat
>(inputLayer, *C1[i], *Conn1Opt[i], 5, 5));
}
This is the code. Here is just 6, but I also need some vector whose size is over 100. I need to convert this vector to a tuple.
A 2D vector of tuples or vector of vectors of tuples is a vector in which each element is a vector of tuples itself. Although a tuple may contain any number of elements for simplicity, a tuple of three elements is considered. Here, dataType1, dataType2, dataType3 can be similar or dissimilar data types.
Method 1: Naive Solution Get the vector to be converted. Create an empty set, to store the result. Iterate through the vector one by one, and insert each element into the set. Print the resultant set.
A rough equivalent of a C++ vector would be a resizing C array (to account for more elements than available). Ergo, the equivalent of an array of vectors would be an array of pointers (an array of arrays wouldn't cut it because of the resizing constraint). int* values[1000];
You could use another Container, empty your queue, append your vector to that container, and create a new queue from that vector; but I'd iterate rather than doing all that. Final answer: No, there is no such method implemented for queues, you could use deque or iterate your vector.
Generally, you cannot convert a vector
to a tuple
. However, if all you're trying to do is make the tuple <f(0), f(1), ..., f(N-1)>
for some N
that is a constant-expression, then that is doable with the index sequence trick:
template <typename F, size_t... Is>
auto gen_tuple_impl(F func, std::index_sequence<Is...> ) {
return std::make_tuple(func(Is)...);
}
template <size_t N, typename F>
auto gen_tuple(F func) {
return gen_tuple_impl(func, std::make_index_sequence<N>{} );
}
Which we can use like:
// make a tuple of the first 10 squares: 0, 1, 4, ..., 81
auto squares = gen_tuple<10>([](size_t i){ return i*i;});
For your specific use-case, that would be:
auto connections = gen_tuple<6>([&](size_t i) {
return new ConvConnection<
decltype(inputLayer),
decltype(*C1[0]),
decltype(*Conn1Opt[0]),
RandomInitialization<arma::mat>,
arma::mat
>(inputLayer, *C1[i], *Conn1Opt[i], 5, 5);
});
If you have C++14, you can do it like this:
template <typename T, std::size_t... Indices>
auto vectorToTupleHelper(const std::vector<T>& v, std::index_sequence<Indices...>) {
return std::make_tuple(v[Indices]...);
}
template <std::size_t N, typename T>
auto vectorToTuple(const std::vector<T>& v) {
assert(v.size() >= N);
return vectorToTupleHelper(v, std::make_index_sequence<N>());
}
Thanks to auto deduction, this is ok. In C++11, without auto deduction, you have to write the return types with trailing decltype
. You also have to implement your own index_sequence
.
You just can't. Because the vector size is known at runtime, but tuple
type (which includes its size) must be known at compile time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With