STL中的模板类pair

来源:互联网 发布:免费速读训练软件 编辑:程序博客网 时间:2024/04/28 12:47

STL的<utility>头文件中描述了一个非常简单的模板类pair,用来表示一个二元组或元素对,并提供了大小比较的比较运算符模板函数。

pair模板类需要两个参数:首元素的数据类型和尾元素的数据类型。pair模板类对象有两个成员:first和second,分别表示首元素和尾元素。

在<utility>中已经定义了pair上的六个比较运算符:<、>、<=、>=、==、!=,其规则是先比较first,first相等时再比较second,这符合大多数应用的逻辑。当然,也可以通过重载这几个运算符来重新指定自己的比较逻辑。

  • 例子程序:
  • [c-sharp] view plaincopy
    1. // map/pair-test.cpp - Show basic use of pair.    
    2. // 2004-02-29 - Fred Swartz - Rodenbach    
    3.    
    4. #include <utility>    
    5. #include <iostream>    
    6. #include <string>    
    7. #include <map>    
    8. using namespace std;    
    9.     
    10. int main() {    
    11.     //-- Declare a pair variable.    
    12.     pair<stringint> pr1;    
    13.         
    14.     //-- Declare and initialize with constructor.    
    15.     pair<stringint> pr2("heaven", 7);    
    16.     cout << pr2.first << "=" << pr2.second << endl;    
    17.     // Prints heaven=7    
    18.         
    19.     //-- Declare and initialize pair pointer.    
    20.     pair<stringint>* prp = new pair<stringint>("yards", 9);    
    21.     cout << prp->first << "=" << prp->second << endl;    
    22.     // Prints yards=9    
    23.         
    24.     //-- Declare map and assign value to keys.    
    25.     map<stringstring> engGerDict;    
    26.     engGerDict["shoe"] = "Schuh";    
    27.     engGerDict["head"] = "Kopf";    
    28.         
    29.     //-- Iterate over map.  Iterator value is a key-value pair.    
    30.     //   Iteration in map is in sorted order.    
    31.     map<stringstring>::const_iterator it;    
    32.     for (it=engGerDict.begin(); it != engGerDict.end(); ++it) {    
    33.         cout << it->first << "=" << it->second << endl;    
    34.     }    
    35.     // Prints head=kopf    
    36.     //        shoe=Schuh    
    37.         
    38.     system("PAUSE");    
    39.     return 0;    
    40. }    
     
  • 除了直接定义一个pair对象外,如果需要即时生成一个pair对象,也可以调用在<utility>中定义的一个模板函数:make_pair。make_pair需要两个参数,分别为元素对的首元素和尾元素。

    • 例子程序:
    • [c-sharp] view plaincopy
      1. // mkpair.cpp    
      2. // compile with: /EHsc    
      3. // Illustrates how to use the make_pair function.    
      4. //    
      5. // Functions: make_pair - creates an object pair containing two data    
      6. //                        elements of any type.    
      7.     
      8. ========make_pair    
      9. #include <utility>    
      10. #include <iostream>    
      11.     
      12. using namespace std;    
      13.     
      14. /* STL pair data type containing int and float  
      15. */    
      16.     
      17. typedef struct pair<int,float> PAIR_IF;    
      18.     
      19. int main(void)    
      20. {    
      21.   PAIR_IF pair1=make_pair(18,3.14f);    
      22.     
      23.   cout << pair1.first << "  " << pair1.second << endl;    
      24.   pair1.first=10;    
      25.   pair1.second=1.0f;    
      26.   cout << pair1.first << "  " << pair1.second << endl;    
      27. }    
       
0 0
原创粉丝点击