pair를 사용하면 두개값 (혹은 사용자정의시 두개이상의 값)을 리턴받을수있다.
컨테이너클래스인 map과 multimap 은 key/value 쌍이라고 불리는 원소를 다루기위해 사용한다.
make_pair(T, T)는 묵시적타입을 제공하지만 부동소수점의 경우에는 double로 변환한다.

이하 예제는 리턴함수와 동적공간생성 , 정적공간생성을 각각수행하고 있다.

#include "stdafx.h"

#include <iostream>
#include <utility>
using namespace std;


pair<int, const char*> OutPut()
{
 return make_pair(20, "hello return");
}

int _tmain(int argc, _TCHAR* argv[])
{
 pair<int, int> * a = new pair<int, int>(1,1);
 cout << a->first << endl;

 pair<int, const char*> b = make_pair(10,"hello");
 cout << b.second << endl;

 cout << OutPut().first << endl;
 cout << OutPut().second;

 delete a;
 return 0;

}




[출처 : http://rickd.egloos.com/4892907]

+ Recent posts