pair 函数 (STL)

来源:互联网 发布:linux查看端口号命令 编辑:程序博客网 时间:2024/05/04 00:57
》》贪心 (区间问题)
》》使用:


pair <int, int> a[11000];
/*法二
typedef pair<int,int> Pii;
Pii a[11000];
*/




  》》  pair 类也是标准库的一部分,它使得我们可以在单个对象内部把相同类型或不同类型的两个值关联起来。为了使用pair 类,我们必须包含下面的头文件


        #include <utility>


    例如


       pair< string, string > author( "James", "Joyce" );


    创建了一个pair 对象author ,它包含两个字符串,分别被初始化为 James 和 Joyce 我们可以用成员访问符号  member access notation 访问pair 中的单个元素,它们的名字为first 和second ,例如


        string firstBook;


        if ( author.first == "James" && author.second == "Joyce" ) 
             firstBook = "Stephen Hero";


    如果我们希望定义大量相同pair 类型的对象 那么最方便的做法就是用typedef  如下所示


        typedef pair< string, string > Authors;


        Authors proust( "marcel", "proust" ); 
        Authors joyce( "james", "joyce" ); 
        Authors musil( "robert", "musil" );


    下面是第二个pair, 一个元素持有对象的名字, 另一个元素持有指向其符号表入口的指针


        // 前向声明 (forward declaration) 
        class EntrySlot; 
        extern EntrySlot* look_up( string ); 
        typedef pair< string, EntrySlot* > SymbolEntry; 
        SymbolEntry current_entry( "author", look_up( "author" ));


        // ...


        if ( EntrySlot *it = look_up( "editor" )) 
         { 
             current_entry.first = "editor"; 
             current_entry.second = it; 
         }

0 0
原创粉丝点击