pair 函数 (STL)

》》贪心 (区间问题)
》》使用:


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; 
         }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值