std::pair

±¸Á¶Ã¼³ª Å¬·¡½º¸¦ ¸¸µéÁö ¾Ê°í µÎ°³ ÀÌ»óÀÇ °ªÀ» ÀÔ·Â ÇÏ°í ½ÍÀ»¶§, std::pair¸¦ »ç¿ëÇÑ´Ù.

std::parit<template T1, template T2> ¿øÇüÀ» °¡Áø´Ù.
3°³¸¦ ÀÔ·ÂÇÏ°í ½ÍÀ» ¶§´Â pair<T1, pair<T2, T3>>¸¦ ÀÔ·ÂÇÑ´Ù.

== ¿¬»êÀÚ¿¡ ´ëÇؼ­ pair¾È¿¡¼­ first ÀÎÀÚ¿Í second ÀÎÀÚ¸¦ ÀÚüÀûÀ¸·Î °Ë»çÇÑ´Ù.
¿¹Á¦¸¦ Âü°í ÇÑ´Ù.

< ¿¬»êÀÚ¿¡ ´ëÇؼ­ pair ÀÚüÀûÀ¸·Î´Â ¾Õ(first)¿¡ ÀÖ´Â °ÍÀÌ ¿ì¼±±ÇÀ» °¡Áø´Ù.

template<class _Ty1,class _Ty2>

inline bool operator<(const pair<_Ty1, _Ty2>& _Left, const pair<_Ty1, _Ty2>& _Right)

{   

    // test if _Left < _Right for pairs

    return (

        _Left.first < _Right.first ||

        !(_Right.first < _Left.first)

        && _Left.second < _Right.second  );

}

´ÙÀ½Àº pair »ç¿ë ¿¹Á¦ÀÌ´Ù.

#include <iostream>

#include <vector>

#include <algorithm>

 

template <typename T>

class Display

{

public :

    void operator()(T& a)

    {

        static int step = 1;

        std::cout << "type" << step++ << ":  " << a.first << ' ' << a.second << std::endl;

    }

};

 

int main ()

{

    typedef std::pair <int, int> IntType;

    std::vector<IntType> array;

 

    array.push_back( IntType( 3, 7));

    array.push_back( IntType( 4, 8));

    array.push_back( IntType( 3, 7));

 

    std::for_each(array.begin(), array.end(), Display<IntType>());

 

    IntType type1 = IntType(3, 7);

    IntType type2 = IntType(4, 8);

    IntType type3 = IntType(3, 7);

 

    if(type1 != type2)

        std::cout << "type1 != type2" << std::endl;

    if(type1 == type3)

        std::cout << "type1 == type3" << std::endl;

 

    std::vector<IntType>::iterator iter = std::find(array.begin(), array.end(), type2);

    if(iter != array.end())

    {

        std::cout << "typd2 std::find ¼º°ø" << std::endl;

    }

    return 0;

}

std::mapÀÇ °´Ã¼¸¦ std::vector¿¡ º¹»çÇÒ ¶§, ±¸Á¶Ã¼¸¦ ¸¸µéÁö ¾Ê°í pair¸¦ »ç¿ëÇÏ¸é °£´ÜÇØÁø´Ù.

std::vector<std::pair <std::string, int>> vecContainer;
std::map< std::string, int> mapContainer;

std::copy(

mapContainer.begin(), mapContainer.end(),  std::back_inserter(vecContainer));

ÀÚ¼¼ÇÑ ÄÚµå´Â ¾Æ·¡¿¡ ;¤Ä¤Ä

#include <iostream>

#include <vector>

#include <algorithm>

#include <string>

#include <map>

 

typedef std::pair <std::string, int> PairType;

 

template <typename T>

class Display

{

public :

    void operator()(T& a)

    {

        static int step = 1;

        std::cout << a.first << ' ' << a.second << std::endl;

    }

};

 

int main ()

{

    std::vector<PairType> vecContainer;

    std::map< std::string, int> mapContainer;

 

    mapContainer.insert(std::make_pair("aaa", 100));

    mapContainer.insert(std::make_pair("bbb", 200));

    mapContainer.insert(std::make_pair("ccc", 300));

 

    //back_inserter´Â ÈÄÀ§ »ðÀÔ ¹Ýº¹ÀÚ ¾î´ðÅÍÀÌ´Ù.

    std::copy( mapContainer.begin(), mapContainer.end(), std::back_inserter(vecContainer));

 

    std::for_each(vecContainer.begin(), vecContainer.end(), Display<PairType>());

 

    return 0;

}

 

 

ÂüÁ¶)
http://ishwat.blogspot.kr/2011/03/stdpair.html?m=1
http://leanu.tistory.com/390
http://blog.daum.net/_blog/BlogTypeView.do?blogid=0PYd8&articleno=111&categoryId=15&regdt=20100330155024