make_pair(변수1,변수2)
: 변수1과 변수2가 들어간 pair를 만들어줍니다.
클래스.first : 클래스의 첫번째 인자를 반환
클래스.second : 클래스의 두번째 인자를 반환
operator로 (==, !=, <, >, <=, >=)가 정의 되어있어서, 사용이 가능합니다.
sort 알고리즘에 의해 정렬이 가능합니다.
(대소 비교 및 sort에의한 정렬에서 : 첫번째 인자 기준, 첫번째가 같으면 두번째인자로 판단)
ex) 좌표 정렬
#include<bits/stdc++.h>
#define endl '\n'
#define FASTio ios_base ::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#include
using namespace std;
void same(pair<int,string> a, pair<int,string> b){
if(a.first==b.first){
cout << "true" << endl;
}
else{
cout << "false" << endl;
}
}
void samesecond(pair<int,string> a,pair<int,string>b){
if(a.second==b.second){
cout << "true" << endl;
}
else{
cout << "false" << endl;
}
}
int main(){
pair<int,string> p1 = make_pair(1,"mommyson");
pair<int,string> p2 = make_pair(2,"mommyson");
make_pair를 통해 p1클래스에 입력
cout << "첫번째 클래스(p1)의 첫번째 인자(first) : " << p1.first <<endl;
cout << "첫번째 클래스(p1)의 두번째 인자(second) : " << p1.second <<endl;
cout << "두번째 클래스(p2)의 첫번째 인자(first) : " << p2.first << endl;
cout << "두번째 클래스(p2)의 두번째 인자(second) : " << p2.second << endl;
cout << "첫번째 클래스와 두번째 클래스의 첫번째 인자가 같을까요? " << endl;
same(p1,p2);
cout << endl;
cout << "첫번째 클래스와 두번째 클래스의 두번째 인자가 같을까요?" << endl;
samesecond(p1,p2);
cout << endl;
return 0;
}
출력 :
첫번째 클래스의 첫번째 인자 : 1
첫번째 클래스의 두번째 인자 : mommyson
두번째 클래스의 첫번째 인자 : 2
두번째 클래스의 두번째 인자 : mommyson
첫번째 클래스와 두번째 클래스의 첫번째 인자가 같을까요?
false
첫번째 클래스와 두번째 클래스의 두번째 인자가 같을까요?
true