Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Question 3
- // Provide a template function “get_vector”, similar to std::get, that given the type X as parameter, returns editable access to the vector of type X in a tuple of vectors.
- #include <iostream>
- #include <tuple>
- #include <vector>
- #include <utility>
- #include <type_traits>
- #include <assert.h>
- using namespace std;
- template < std::size_t... N , typename... T >
- auto tail_impl( std::index_sequence<N...> , std::tuple<T...> t ) {
- return std::make_tuple( std::get<N+1>(t)... );
- }
- template <class F, class... R >
- tuple<R...> tail( std::tuple<F,R...> t ) {
- return tail_impl( std::make_index_sequence<sizeof...(R)>() , t );
- }
- template<class T>
- void print_vec(const vector<T>& v) {
- for(auto& x:v) {
- cout << x << '\n';
- }
- cout << endl;
- }
- template<typename X, typename T>
- vector<X>& get_vector(T& t) {
- return get<vector<X>>(t);
- }
- int main( int argc, const char *argv[] ) {
- auto a = vector<int>({4,5,3});
- auto b = vector<char>({'s', 'a', 'g', 'a'});
- auto c = vector<double>({3.232,32.3,2.33});
- auto t = make_tuple(a,b,c);
- vector<char>& f = get_vector<char>(t);
- f.push_back('r');
- print_vec(f);
- print_vec(get<0>(tail(t)));
- return 0;
- }
Add Comment
Please, Sign In to add comment