pair 클래스는 STL에서 두 객체를 하나의 객체로 취급 할 수 있게 묶어주는 클래스다. pair의 사전적의미처럼 쌍 을 표현할 때 사용하며 <utility>
헤더에 존재한다.
template <class T1, class T2> struct pair;
template <typename T1, typename T2> struct pair;
#include<iostream>
#include<utility>
#include<string>
using namespace std;
int main(void){
pair<int, string> p1 = make_pair(1,"test1");
pair<int, string> p2 = make_pair(3,"test3");
cout << "p1.first : " << p1.first << endl;
// p1.first : 1
cout << "p1.second : " << p1.second << endl;
// p1.second : test1
return 0;
}
#include<iostream>
#include<utility>
#include<string>
#include<vector>
using namespace std;
int main(void){
vector<pair<int, string> > v;
v.push_back(pair<int, string>(3, "Dok2"));
v.push_back(pair<int, string>(6, "DMask"));
v.push_back(pair<int, string>(1, "Tiger JK"));
v.push_back(pair<int, string>(4, "Block"));
v.push_back(pair<int, string>(2, "banana"));
v.push_back(pair<int, string>(2, "apple"));
vector<pair<int, string> >::iterator i;
for(i = v.begin(); i != v.end(); i++){
cout << "[" << i->first << "," << i->second << "]" << endl;
}
return 0;
}
#include<algorithm>
using namespace std;
int main(void){
vector<pair<int, string> > v;
v.push_back(pair<int, string>(3, "Dok2"));
v.push_back(pair<int, string>(6, "DMask"));
v.push_back(pair<int, string>(1, "Tiger JK"));
v.push_back(pair<int, string>(4, "Block"));
v.push_back(pair<int, string>(2, "banana"));
v.push_back(pair<int, string>(2, "apple"));
cout << "=== After sort === " << endl;
sort(v.begin(), v.end());
for(iter = v.begin(); iter != v.end(); iter++){
cout << "[" << iter->first << "," << iter->second << "]" << endl;
}
return 0;
}