跳到正文

个人记录

STL pair 实现

sjr3065335594约 6 分钟
原文
文章信息与版本

文章信息

发布
2023年11月1日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 6 分钟

版本

  1. v1当前

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

模型

C++
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。

C++
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()

直接交换。

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

各种比较符号重载

很简单。

C++
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 就行了。

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

完整代码

C++
#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);
	}
};
}
原文
存档评论1 条抓取于