This class couples together a pair of values, which may be of different types (T1 and T2). The individual values can be accessed through its public members first and second.
示例:
#include <utility> // std::pair, std::make_pair
#include <string> // std::string
#include <iostream> // std::cout
int main () {
std::pair <std::string,int> planet, homeplanet;
planet = std::make_pair("Earth",6371);
homeplanet = planet;
std::cout << "Home planet: " << homeplanet.first << '\n';
std::cout << "Planet size: " << homeplanet.second << '\n';
return 0;
}
Output:
Home planet: Earth
Planet size: 6371
#include <utility> // std::pair
#include <iostream> // std::cout
int main () {
std::pair<int,char> foo (10,'a');
std::pair<int,char> bar (90,'z');
foo.swap(bar);
std::cout << "foo contains: " << foo.first;
std::cout << " and " << foo.second << '\n';
return 0;
}
foo contains: 90 and z