C++ 用 std::get<> 访问元组

 

C++  用 std::get<> 访问元组

 

#include <iostream>
#include <tuple>

int main() {
    // Creating a tuple
    std::tuple<int, double, std::string> myTuple(42, 3.14, "Hello");

    // Accessing elements using std::get<>
    int intValue = std::get<0>(myTuple);
    double doubleValue = std::get<1>(myTuple);
    std::string stringValue = std::get<2>(myTuple);

    // Displaying the values
    std::cout << "First element: " << intValue << std::endl;
    std::cout << "Second element: " << doubleValue << std::endl;
    std::cout << "Third element: " << stringValue << std::endl;

    return 0;
}

 

posted @ 2023-12-12 14:55  He_LiangLiang  阅读(124)  评论(0编辑  收藏  举报