【C++编程】std::get用法

 pair

1. 示例

#include <iostream>
#include <utility>

int main()
{
    auto p = std::make_pair(1, 3.14);
    std::cout << '(' << std::get<0>(p) << ", " << std::get<1>(p) << ")\n";
    std::cout << '(' << std::get<int>(p) << ", " << std::get<double>(p) << ")\n";
}

 

#include <iostream>
#include <string>
#include <tuple>

int main()
{
    auto t = std::make_tuple(1, "Foo", 3.14);

    // Index-based access
    std::cout << "( " << std::get<0>(t)
              << ", " << std::get<1>(t)
              << ", " << std::get<2>(t)
              << " )\n";

    // Type-based access (C++14 or later)
    std::cout << "( " << std::get<int>(t)
              << ", " << std::get<const char*>(t)
              << ", " << std::get<double>(t)
              << " )\n";
}

 

posted @ 2021-10-13 21:33  苏格拉底的落泪  阅读(276)  评论(0编辑  收藏  举报