qfedu-cpp-level/day10/stl_set_demo/h6.cpp

40 lines
1002 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 对组(pair)
// 将一对值组合成一个值,这一对值可以具有不同的数据类型,两个值可
// 以分别用 pair 的两个公有属性 first 和 second 访问。
// 类模板template<class T1, class T2> class pair
// 用法一:
// pair<string, int> pair1(string("name"), 20);
// cout << pair1.first << endl;
// cout << pair1.second << endl;
// 用法二:
// pair<string, int> pair2 = make_pair("name", 30);
// cout << pair2.first << endl;
// cout << pair2.second << endl;
// 用法三:
// pair<string, int> pair3 = pair2; // 拷贝构造函数
// cout << pair3.first << endl;
// cout << pair3.second << endl;
// 如:
#include <iostream>
#include <set>
#include <algorithm>
using namespace std;
int main()
{
pair<int, string> p1(1, "disen");
pair<int, string> p2 = make_pair(2, "lucy");
cout << "id = " << p1.first << ", name = " << p1.second << endl;
cout << "id = " << p2.first << ", name = " << p2.second << endl;
return 0;
}