iterator on std 3a 3atuple

Solutions on MaxInterview for iterator on std 3a 3atuple by the best coders in the world

showing results for - "iterator on std 3a 3atuple"
Kellie
10 May 2018
1#include <tuple>
2#include <iostream>
3
4int main()
5{
6    std::tuple t{42, 'a', 4.2}; // Another C++17 feature: class template argument deduction
7    std::apply([](auto&&... args) {((std::cout << args << '\n'), ...);}, t);
8}
9
Francisco
15 Feb 2019
1{
2    auto tup = std::make_tuple(0, 'a', 3.14);
3    template for (auto elem : tup)
4        std::cout << elem << std::endl;
5}
6
Matthew
29 Apr 2020
1#include <tuple>
2#include <utility>
3
4template<std::size_t N>
5struct tuple_functor
6{
7    template<typename T, typename F>
8    static void run(std::size_t i, T&& t, F&& f)
9    {
10        const std::size_t I = (N - 1);
11        switch(i)
12        {
13        case I:
14            std::forward<F>(f)(std::get<I>(std::forward<T>(t)));
15            break;
16
17        default:
18            tuple_functor<I>::run(i, std::forward<T>(t), std::forward<F>(f));
19        }
20    }
21};
22
23template<>
24struct tuple_functor<0>
25{
26    template<typename T, typename F>
27    static void run(std::size_t, T, F){}
28};
29
Ariana
20 Feb 2016
1std::apply([](auto ...x){std::make_tuple(x.do_something()...);} , the_tuple);
2
Fabiana
12 Jul 2016
1#include <tuple>
2#include <iostream>
3#include <boost/hana.hpp>
4#include <boost/hana/ext/std/tuple.hpp>
5
6struct Foo1 {
7    int foo() const { return 42; }
8};
9
10struct Foo2 {
11    int bar = 0;
12    int foo() { bar = 24; return bar; }
13};
14
15int main() {
16    using namespace std;
17    using boost::hana::for_each;
18
19    Foo1 foo1;
20    Foo2 foo2;
21
22    for_each(tie(foo1, foo2), [](auto &foo) {
23        cout << foo.foo() << endl;
24    });
25
26    cout << "foo2.bar after mutation: " << foo2.bar << endl;
27}
28
Niko
08 Mar 2018
1template <size_t ...I>
2struct index_sequence {};
3
4template <size_t N, size_t ...I>
5struct make_index_sequence : public make_index_sequence<N - 1, N - 1, I...> {};
6
7template <size_t ...I>
8struct make_index_sequence<0, I...> : public index_sequence<I...> {};
9
Giovanni
05 Aug 2019
1// prints every element of a tuple
2template<size_t I = 0, typename... Tp>
3void print(std::tuple<Tp...>& t) {
4    std::cout << std::get<I>(t) << " ";
5    // do things
6    if constexpr(I+1 != sizeof...(Tp))
7        print<I+1>(t);
8}
9