C++:pair,make_pair,value_type的示例

01 示例

#include <iostream>
#include <string>
#include <map>
#include <utility>        // make_pair,pair

int main(void)
{
    map<int,string> mapTemp;

    // insert a new element
    mapTemp.insert(make_pair(1,"name_01_FirMtd"));
    mapTemp.insert(pair<int,string>(2,"name_01_SedMtd"));
    mapTemp.insert(map<int,string>::value_type(3,"name_01_TrdMtd"));

    // insert a existed element,it is invalid;
    mapTemp.insert(make_pair(1,"name_02_FirMtd"));
    mapTemp.insert(pair<int,string>(2,"name_02_SedMtd"));
    mapTemp.insert(map<int,string>::value_type(3,"name_02_TrdMtd"));

   //
   map<int,string>::iterator itMap;
   for(itMap= mapTemp.begin();itMap != mapTemp.end(); itMap++)
   {
       cout<<itMap->second<<endl;
   }
   return 0;   
}

02 function
A template function that you can use to construct objects of type pair, where the component types are automatically chosen based on the data types that are passed as parameters

Val1:Value that initializes the first element of pair.
Val2:Value that initializes the second element of pair.
Return:The pair object that's constructed: pair(Val1, Val2).

你可能感兴趣的:(C++:pair,make_pair,value_type的示例)