1. stl_pair.h
  2. // Filename: stl_pair.h
  3.  
  4. // Comment By: 凝霜
  5. // E-mail: mdl2009@vip.qq.com
  6. // Blog: http://blog.csdn.net/mdl13412
  7.  
  8. /*
  9. *
  10. * Copyright (c) 1994
  11. * Hewlett-Packard Company
  12. *
  13. * Permission to use, copy, modify, distribute and sell this software
  14. * and its documentation for any purpose is hereby granted without fee,
  15. * provided that the above copyright notice appear in all copies and
  16. * that both that copyright notice and this permission notice appear
  17. * in supporting documentation. Hewlett-Packard Company makes no
  18. * representations about the suitability of this software for any
  19. * purpose. It is provided "as is" without express or implied warranty.
  20. *
  21. *
  22. * Copyright (c) 1996,1997
  23. * Silicon Graphics Computer Systems, Inc.
  24. *
  25. * Permission to use, copy, modify, distribute and sell this software
  26. * and its documentation for any purpose is hereby granted without fee,
  27. * provided that the above copyright notice appear in all copies and
  28. * that both that copyright notice and this permission notice appear
  29. * in supporting documentation. Silicon Graphics makes no
  30. * representations about the suitability of this software for any
  31. * purpose. It is provided "as is" without express or implied warranty.
  32. */
  33.  
  34. /* NOTE: This is an internal header file, included by other STL headers.
  35. * You should not attempt to use it directly.
  36. */
  37.  
  38. #ifndef __SGI_STL_INTERNAL_PAIR_H
  39. #define __SGI_STL_INTERNAL_PAIR_H
  40.  
  41. __STL_BEGIN_NAMESPACE
  42.  
  43. // pair只是一个wraper, 所以要提供最佳效率
  44. // 使用struct的原因是我们要能方便的存取内部元素
  45. // pair在关联式容器中的使用极为广泛, 其本身也可以嵌套使用
  46. template <class T1, class T2>
  47. struct pair
  48. {
  49. typedef T1 first_type;
  50. typedef T2 second_type;
  51.  
  52. T1 first;
  53. T2 second;
  54. pair() : first(T1()), second(T2()) {}
  55. pair(const T1& a, const T2& b) : first(a), second(b) {}
  56.  
  57. // 此版本并未提供operator =()的支持, 个人认为应该提供
  58.  
  59. #ifdef __STL_MEMBER_TEMPLATES
  60. // 允许使用兼容的pair进行复制构造
  61. template <class U1, class U2>
  62. pair(const pair<U1, U2>& p) : first(p.first), second(p.second) {}
  63. #endif
  64. };
  65.  
  66. // 只有当pair中的两个成员均相等时, 才判定两个pair相等
  67. // 使用自定义类型时最好提供operator ==重载
  68. template <class T1, class T2>
  69. inline bool operator==(const pair<T1, T2>& x, const pair<T1, T2>& y)
  70. {
  71. return x.first == y.first && x.second == y.second;
  72. }
  73.  
  74. // 连个pair进行比较操作时, 以第一个元素为主, 如果第一个元素不能决定表达式的值
  75. // 那么再进行第二个元素的比较
  76. // 使用自定义类型时最好提供operator <重载
  77. template <class T1, class T2>
  78. inline bool operator<(const pair<T1, T2>& x, const pair<T1, T2>& y)
  79. {
  80. return x.first < y.first || (!(y.first < x.first) && x.second < y.second);
  81. }
  82.  
  83. // 至于为什么没有提供operator !=, >, >=, <=
  84. // 这个是因为其在<stl_relops.h>中有实现, 其只依赖operator <和==
  85. // 所以在此特化operator ==, <就能满足要求
  86. // 提供<stl_relops.h>的作用是如果需要特化operator XXX
  87. // 那么我们仅需要特化operator ==和<即可同时重载所有operator
  88.  
  89. // 这里使用了RVO(Return Value Optimization)机制, 如果编译器支持,
  90. // 则可以消除临时对象的构造和析构负担
  91. // 详细细节见<Inside The C++ Object Model>
  92. template <class T1, class T2>
  93. inline pair<T1, T2> make_pair(const T1& x, const T2& y)
  94. {
  95. return pair<T1, T2>(x, y);
  96. }
  97.  
  98. __STL_END_NAMESPACE
  99.  
  100. #endif /* __SGI_STL_INTERNAL_PAIR_H */
  101.  
  102. // Local Variables:
  103. // mode:C++
  104. // End:

stl_pair.h的更多相关文章

  1. STL源代码剖析 容器 stl_map.h

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie map ------------------------------------------ ...

  2. stl_algobase.h

    stl_algobase.h // Filename: stl_algobase.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // Blog: ...

  3. stl_stack.h

    stl_stack.h // Filename: stl_stack.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // Blog: http:/ ...

  4. stl_queue.h

    stl_queue.h // Filename: stl_queue.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // Blog: http:/ ...

  5. STL源代码剖析——基本算法stl_algobase.h

    前言 在STL中.算法是常常被使用的,算法在整个STL中起到很关键的数据.本节介绍的是一些基本算法,包括equal.fill.fill_n,iter_swap.lexicographical_comp ...

  6. construction const parameter问题 构造函数const引用参数问题

    工程在window下编译没有任何问题, 但是在linux(CentOS6)下编译就老是报错 C++ 编译器已升级到最新版 6.1.0 错误如下: In file included /bits/stl_ ...

  7. NDK(17)让ndk支持完整C++,exception,rtti,

    C++ Support The Android platform provides a very minimal C++ runtime support library (/system/lib/li ...

  8. 【转载】【挖掘Treap的潜力】

    原帖: http://fanhq666.blog.163.com/blog/static/819434262011021105212299/ 你的Treap能支持以下操作吗?1.区间增减 2.区间求最 ...

  9. thread - 传递引用参数

    当给 thread 的执行函数传递指针参数时,没有任何问题,但是如果想传递引用,按照普通函数的调用方法会遇到编译失败: #include <iostream> #include <t ...

随机推荐

  1. LNMP环境搭建(三:PHP)

    1.获取php源码 # cd /usr/local/src/ # wget http://cn2.php.net/get/php-7.0.15.tar.gz/from/this/mirror 2.解压 ...

  2. Web客户端语言HTML、XHTML和XML相关知识介绍

    HTML简介 HTML(Hyper Text Mark-up Language)即超文本标记语言或超文本链接标示语言,是目前网络上应用最为广泛的语言,也是构成网页文档的主要语言.HTML文本是由HTM ...

  3. 爬虫入门【7】Python-文件的读写和JSON

    文本文档的读写 最重要的open()方法将返回一个file对象,经常使用的两个参数为open(filename,mode) 其中,filename为file保存的地址,可以是本地地址,相对地址或者绝对 ...

  4. 前端开发中js变量定义及命名的规范建议

    关于变量定义及命名 现在谈谈关于变量及方法等的命名,没有硬性规定,但为了规范,遵循一些约定还是很有必要的. 变量定义:好的做法是把将要使用的变量名用一个var关键字一并定义在代码开头,变量名间用逗号隔 ...

  5. HTML-Table-Td固定宽度使内容换行

    table style="TABLE-LAYOUT: fixed;" td style="WORD-WRAP: break-word;WIDTH:200px;"

  6. ASP向上取整

    <%Function Ceil(value)    Dim return    return = int(value)    Cei2=value-return    if Cei2>0 ...

  7. 我的Android进阶之旅------>关于android:layout_weight属性的一个面试题

    最近碰到一个面试题,按照下图,由Button和EditText组成的界面下厨布局代码,解决这题目需要使用android:layout_weight的知识. 首先分析上图所示的界面可以看成一下3个部分. ...

  8. 模仿jquery框架源码 -生长---跨域访问

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  9. POJ - 3414 Pots 【BFS】

    题目链接 http://poj.org/problem?id=3414 题意 给出两个杯子 容量分别为 A B 然后给出C 是目标容量 有三种操作 1 将一个杯子装满 2.将一个杯子全都倒掉 3.将一 ...

  10. 剑指offer——圆圈中最后剩下的数字

    1.如果通过环形列表去模拟圆圈的话,最后时间复杂度为O(mn),而且还需要一个辅助链表来模拟圆圈,空间复杂度为O(n). 2.通过找出递推公式的方法,求得递推公式为 时间复杂度为O(n),空间复杂度为 ...