pair和make_pair用法

[cpp] view plaincopyprint?

  1. // pair简单讲就是将两个数据整合成一个数据  
  2. // 本质上是有first, second两个成员变量的结构体  
  3. extern void test_pair()  
  4. {  
  5.     // pair两种构造的方法  
  6.     // 方法1  
  7.     std::pairdouble>("This is a StringTest0.", 9.7);    // 浮点数默认是double, float的话有会警告。  
  8.     std::pairdouble> pA("This is a StringTest.", 9.7);  
  9.     // 方法2  
  10.     std::pairdouble> pB;  
  11.     pB = std::make_pair("This is a StringTest.", 9.9);  
  12.       
  13.     // pair的输出  
  14.     std::cout << pA.first << std::endl;  
  15.     std::cout << pA.second << std::endl;  
  16.   
  17.     // 结合map的使用  
  18.     std::mapdouble> mA;  
  19.     mA.insert(pA);  
  20.     mA.insert(pB);  
  21.   
  22.     for (std::mapdouble>::iterator it = mA.begin(); it != mA.end(); ++it)  
  23.     {  
  24.         std::cout << "First Member:  " << it->first << std::endl;  
  25.         std::cout << "Second Member: " << it->second << std::endl;  
  26.     }  
  27. }  

你可能感兴趣的:(C/C++)