【pair的初始化】pair < int , string > a;
//定义一个标识符为a的pair,第一个值的类型为int,第二个值的类型为string
【pair的调用】std::pair <std::string,double> product2 ("tomatoes",2.30);//直接赋值
std::pair <std::string,double> product3 (product2);//将其他pair的值复制过来
【pair的赋值】std::cout << "The price of " << product2.first << " is $" << product2.second << '\n';
【使用typedef进行简化】product1 = std::make_pair(std::string("lightbulbs"),0.99);
关于pair类型的内容大概就是这些,最后放一个使用样例:typedef pair<string, string> author;
author pro("May", "Lily");
author joye("James", "Joyce");
// pair::pair example
#include <utility> // std::pair, std::make_pair
#include <string> // std::string
#include <iostream> // std::cout
int main () {
std::pair <std::string,double> product1; // default constructor
std::pair <std::string,double> product2 ("tomatoes",2.30); // value init
std::pair <std::string,double> product3 (product2); // copy constructor
product1 = std::make_pair(std::string("lightbulbs"),0.99); // using make_pair (move)
product2.first = "shoes"; // the type of first is string
product2.second = 39.90; // the type of second is double
std::cout << "The price of " << product1.first << " is $" << product1.second << '\n';
std::cout << "The price of " << product2.first << " is $" << product2.second << '\n';
std::cout << "The price of " << product3.first << " is $" << product3.second << '\n';
return 0;
}