C++ std::get fails

后端 未结 4 1653
抹茶落季
抹茶落季 2021-01-12 20:48

How do I use a variable to index into a tuple using std::get<>? I have the following code:

#include 
#include 
using n         


        
4条回答
  •  暖寄归人
    2021-01-12 21:40

    Any idea how to make that work?

    Option 1

    Use compile time constants to access the std::tuple.

    cout << "#" << 1 << ":" << get<0>(data) << endl;
    cout << "#" << 2 << ":" << get<1>(data) << endl;
    

    Option 2

    Use a container type whose elements can be accessed using an index at run time.

    std::vector data{5, 10};
    

    or

    std::array data{5, 10};
    

提交回复
热议问题