pair隨筆

<STL> pair隨筆

#include <iostream>
#include <string>
using namespace std;


template<class T1,class T2>
class pair1
{
 public:
    T1 first;
    T2 second;
    //pair1():first(T1()),second(T2()){}
    pair1():first(),second(){}
    pair1(const T1& t1,const T2& t2):first(t1),second(t2){}
};


int main()
{
    pair1<int,int>p1;
    pair1<string,int>p2("hicjiajia",425);
    cout<<p1.first<<" "<<p1.second<<endl;
    cout<<p2.first<<" "<<p2.second<<endl;
    system("pause");
    return 0;
}


// Filename:    stl_pair.h


// Comment By:  凝霜
// E-mail:      [email protected]
// Blog:        http://blog.csdn.net/mdl13412


/*
 *
 * Copyright (c) 1994
 * Hewlett-Packard Company
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  Hewlett-Packard Company makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 *
 *
 * Copyright (c) 1996,1997
 * Silicon Graphics Computer Systems, Inc.
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  Silicon Graphics makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 */


/* NOTE: This is an internal header file, included by other STL headers.
 *   You should not attempt to use it directly.
 */


#ifndef __SGI_STL_INTERNAL_PAIR_H
#define __SGI_STL_INTERNAL_PAIR_H


__STL_BEGIN_NAMESPACE


// pair只是一個wraper, 所以要提供最佳效率
// 使用struct的原因是我們要能方便的存取內部元素
// pair在關聯式容器中的使用極爲廣泛, 其本身也可以嵌套使用
template <class T1, class T2>
struct pair
{
  typedef T1 first_type;
  typedef T2 second_type;


  T1 first;
  T2 second;
  pair() : first(T1()), second(T2()) {}
  pair(const T1& a, const T2& b) : first(a), second(b) {}


  // 此版本並未提供operator =()的支持, 個人認爲應該提供


#ifdef __STL_MEMBER_TEMPLATES
  // 允許使用兼容的pair進行復制構造
  template <class U1, class U2>
  pair(const pair<U1, U2>& p) : first(p.first), second(p.second) {}
#endif
};


// 只有當pair中的兩個成員均相等時, 才判定兩個pair相等
// 使用自定義類型時最好提供operator ==重載
template <class T1, class T2>
inline bool operator==(const pair<T1, T2>& x, const pair<T1, T2>& y)
{
  return x.first == y.first && x.second == y.second;
}


// 連個pair進行比較操作時, 以第一個元素爲主, 如果第一個元素不能決定表達式的值
// 那麼再進行第二個元素的比較
// 使用自定義類型時最好提供operator <重載
template <class T1, class T2>
inline bool operator<(const pair<T1, T2>& x, const pair<T1, T2>& y)
{
  return x.first < y.first || (!(y.first < x.first) && x.second < y.second);
}


// 至於爲什麼沒有提供operator !=, >, >=, <=
// 這個是因爲其在<stl_relops.h>中有實現, 其只依賴operator <和==
// 所以在此特化operator ==, <就能滿足要求
// 提供<stl_relops.h>的作用是如果需要特化operator XXX
// 那麼我們僅需要特化operator ==和<即可同時重載所有operator


// 這裏使用了RVO(Return Value Optimization)機制, 如果編譯器支持,
// 則可以消除臨時對象的構造和析構負擔
// 詳細細節見<Inside The C++ Object Model>
template <class T1, class T2>
inline pair<T1, T2> make_pair(const T1& x, const T2& y)
{
  return pair<T1, T2>(x, y);
}


__STL_END_NAMESPACE


#endif /* __SGI_STL_INTERNAL_PAIR_H */


// Local Variables:
// mode:C++
// End:
發佈了707 篇原創文章 · 獲贊 48 · 訪問量 97萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章