文章转载自:http://www.cnblogs.com/Nimeux/archive/2010/10/05/1844191.html
std::pair主要的作用是将两个数据组合成一个数据,两个数据可以是同一类型或者不同类型。例如std::pair
template pair make_pair(T1 a, T2 b) { return pair(a, b); }
std::pair(1, 1.1);
std::make_pair(1, 1.1);
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 #include
#include
#include
using namespace std;
int main () {
pair product1 ("tomatoes",3.25);
pair product2;
pair product3;
product2.first = "lightbulbs"; // type of first is string
product2.second = 0.99; // type of second is double
product3 = make_pair ("shoes",20.0);
cout << "The price of " << product1.first << " is $" << product1.second << "\n";
cout << "The price of " << product2.first << " is $" << product2.second << "\n";
cout << "The price of " << product3.first << " is $" << product3.second << "\n";
return 0;
}
输出的结果是:
The price of tomatoes is $3.25
The price of lightbulbs is $0.99
The price of shoes is $20