STL pair 实现

· · 个人记录

这个比较简单,我完全是自己手写的,如有问题请指出。

模型

template<typename T1,typename T2>
class pair {
public:
    typedef T1 _type_of_first ;
    typedef T2 _type_of_second ;
    _type_of_first first;
    _type_of_first second;

};

构造函数

实现了三个,一个什么都没有,一个给初始值,一个拷贝一个 pair。

explicit pair()
    :first(0),second(0) {}
explicit pair(_type_of_first _val_1,_type_of_second _val_2)
    :first(_val_1),second(_val_2) {}
pair(pair<_type_of_first,_type_of_second>&pir)
    :first(pir.first),second(pir.second) {}

swap()

直接交换。

void swap(pair<_type_of_first,_type_of_second> &pir) {
    std::swap(first,pir.first);
    std::swap(second,pir.second);
}

各种比较符号重载

很简单。

bool operator == (const pair<_type_of_first,_type_of_second> &pir) const {
    if(pir.first==first&&pir.second==second)return true;
    return false;
}
bool operator != (const pair<_type_of_first,_type_of_second> &pir) const {
    return (!(pir==*this));
}
bool operator < (const pair<_type_of_first,_type_of_second> &pir) const {
    if(pir.first==first)return second<pir.second;
    return first<pir.first;
}
bool operator <= (const pair<_type_of_first,_type_of_second> &pir) const {
    return ((*this==pir)||(*this<pir));
}
bool operator > (const pair<_type_of_first,_type_of_second> &pir) const {
    return (!(*this<=pir));
}
bool operator >= (const pair<_type_of_first,_type_of_second> &pir) const {
    return (!(*this<pir));
}

make_pair

直接返回一个 pair 就行了。

template<typename T1,typename T2>
pair<T1,T2> make_pair(T1 first,T2 second) {
    pair<T1,T2>pir;
    pir.first=first;
    pir.second=second;
    return pir;
}

完整代码

#include<iostream>
#include<cassert>
using namespace std;
namespace Pair {
template<typename T1,typename T2>
class pair {
    public:
        typedef T1 _type_of_first ;
        typedef T2 _type_of_second ;
        _type_of_first first;
        _type_of_first second;
    explicit pair()
        :first(0),second(0) {}
    explicit pair(_type_of_first _val_1,_type_of_second _val_2)
        :first(_val_1),second(_val_2) {}
    pair(pair<_type_of_first,_type_of_second>&pir)
        :first(pir.first),second(pir.second) {}
    bool operator == (const pair<_type_of_first,_type_of_second> &pir) const {
        if(pir.first==first&&pir.second==second)return true;
        return false;
    }
    bool operator != (const pair<_type_of_first,_type_of_second> &pir) const {
        return (!(pir==*this));
    }
    bool operator < (const pair<_type_of_first,_type_of_second> &pir) const {
        if(pir.first==first)return second<pir.second;
        return first<pir.first;
    }
    bool operator <= (const pair<_type_of_first,_type_of_second> &pir) const {
        return ((*this==pir)||(*this<pir));
    }
    bool operator > (const pair<_type_of_first,_type_of_second> &pir) const {
        return (!(*this<=pir));
    }
    bool operator >= (const pair<_type_of_first,_type_of_second> &pir) const {
        return (!(*this<pir));
    }
    void swap(pair<_type_of_first,_type_of_second> &pir) {
        std::swap(first,pir.first);
        std::swap(second,pir.second);
    }
};
}