代码点读机 / C++ / std::make_pair

std::make_pair C++ · STL

工厂函数,根据实参自动推断类型并构造一个 std::pair,不用手写模板参数。

怎么用

auto p = std::make_pair(a, b);

小例子

auto p = std::make_pair(1, std::string("a"));
std::cout << p.second;

运行结果

a

提醒:C++17 起 pair 的构造函数支持类模板参数推断,make_pair 仍常用

官方文档:https://en.cppreference.com/w/cpp/utility/pair/make_pair

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← C++全部词条