#include
#include
#include
// 方法2 pair
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
pair anon; // 包括两个字符串
pair word_count; // 包括字符串和整数
pair > line; // 包括字符串和一个int容器
pair author("James", "Joyce"); // 定义成员时初始化
cout << author.first << " - " << author.second << endl;
string firstBook; // 使用 . 訪问和測试pair数据成员
if (author.first == "James" && author.second == "Joyce") {
firstBook = "Stephen Hero";
cout << firstBook << endl;
}
typedef pair Author; // 简化声明一个作者pair类型
Author proust("Marcel", "Proust");
Author Joyce("James", "Joyce");
pair next_auth;
string first, last;
while (cin >> first >> last) {
// 使用make_pair函数生成一个新pair对象
next_auth = make_pair(first, last);
// 使用make_pair函数,等价于以下这句
next_auth = pair (first, last);
cout << next_auth.first << " - " << next_auth.second << endl;
if (next_auth.first == next_auth.second)
break; // 输入两个相等,退出循环
}
cout << "由于pair的数据成员是共同拥有的。因而能够直接读取输入" << endl;
while (cin >> next_auth.first >> next_auth.second) {
cout << next_auth.first << " - " << next_auth.second << endl;
if (next_auth.first == next_auth.second)
break;
}
return 0;
}
临时仅仅会这么简单的使用方法。刚学到的……