c++版Pair实现

写在前面:小生纯业余选手,开此博仅仅是为了积累,纯当笔记来用。如有看官光临小生博客,请不要相信我的代码就是正确的。如果您发现了错误也恳请耽误您一点时间,请您在下面指出来,不胜感激!


这里的pair与SGI STL里边的pair一样都是键值对,有的书上也叫“字典”(dictionary),不管这种结构叫什么,它都是为了更好地管理数据。在实现的时候遇到了各种各样的麻烦,最后不得不搬出书上的代码。这里的实现要包含list代码文件,键值对是作为一个单元放在某一容器中,下面代码选择的是list容器。


#include "list.h"
using namespace std;

template<typename Key,typename E>
class Dictionary
{
private:
	void operator=(const Dictionary&){}
	Dictionary(const Dictionary&){}
public:
	Dictionary(){}
	~Dictionary(){}

	virtual void clear() = 0;
	virtual void insert(const Key& k,const E& e) = 0;
	virtual E remove(const Key& k) = 0;
	virtual E removeAny() = 0;
	virtual E find(const Key&)const = 0;
	virtual int size() = 0;
};

template<typename Key,typename E>
class KVpair
{
private:
	Key k;
	E e;
public:
	KVpair(){}
	KVpair(Key kval,E eval):k(kval),e(eval){}
	KVpair(const KVpair& o):k(o.k),e(o.e){}

	void operator =(const KVpair& o)
	{
		k = o.k;
		e = o.e;
	}

	Key key()
	{
		return k;
	}
	void setKey(Key ink)
	{
		k = ink;
	}

	E value()
	{
		return e;
	}
};

template<typename Key,typename E>
class UALdict : public Dictionary<Key,E>
{
private:
	AList<KVpair<Key,E> >* list;
public:
	UALdict(int size = 10)
	{
		list = new AList<KVpair<Key,E> >(size);
	}
	~UALdict()
	{
		delete list;
	}
	void clear()
	{
		list->clear();
	}
	void insert(const Key& k,const E& e)
	{
		KVpair<Key,E> temp(k,e);
		list->append(temp);
	}
	E remove(const Key &k)
	{
		E temp = find(k);
		if(temp != NULL)
			list->remove();
		return temp;
	}
	E removeAny()
	{
		assert(size() != 0);
		list->moveToEnd();
		list->prev();
		KVpair<Key,E> e = list->remove();
		return e.value();
	}
	E find(const Key& k)const
	{
		for(list->moveToStart();list->currPos() < list->length();list->next())
		{
			KVpair<Key,E> temp = list->getValue();
			if(k == temp.key())
				return temp.value();
		}
		return NULL;
	}
	int size()
	{
		return list->length();
	}
};

template<typename Key,typename E>
class SAList : protected AList<KVpair<Key,E> >
{
public:
	SAList(int size = 10):AList<KVpair<Key,E> >(size){}
	~SAList(){}
	void insert(KVpair<Key,E>& it)
	{
		KVpair<Key,E> curr;
		for(moveToStart();currPos() < length();next())
		{
			curr = getValue();
			if(curr.key()>it.key())
				break;
		}
		AList<KVpair<Key,E> >::insert(it);
	}

	AList<KVpair<Key,E> >::clear();
	AList<KVpair<Key,E> >::remove();
	AList<KVpair<Key,E> >::moveToStart();
	AList<KVpair<Key,E> >::moveToEnd();
	AList<KVpair<Key,E> >::prev();
	AList<KVpair<Key,E> >::next();
	AList<KVpair<Key,E> >::length();
	AList<KVpair<Key,E> >::currPos();
	AList<KVpair<Key,E> >::moveToPos();
	AList<KVpair<Key,E> >::getValue();
};

template<typename Key,typename E>
class SALdict : public Dictionary<Key,E>
{
private:
	SAList<Key,E>* list;
public:
	SALdict(int size = 10)
	{
		list = new SAList<Key,E>(size);
	}
	~SALdict(){}
	void clear()
	{
		list->clear();
	}
	void insert(const Key& k,const E& e)
	{
		KVpair<Key,E> temp(k,e);
		list->insert(temp);
	}
	E remove(const Key& k)
	{
		E temp = find(k);
		if(temp != NULL)
			list->remove();
		return temp;
	}
	E removeAny()
	{
		assert(size() != 0);
		list->moveToEnd();
		list->prev();
		KVpair<Key,E> e = list->remove();
		return e.value();
	}
	E find(const Key& k)const
	{
		int l = -1;
		int r = list->length();
		while (l+1 != r)
		{
			int i = (l+r)/2;
			list->moveToPos(i);
			KVpair<Key,E> temp = list->getValue();
			if(k < temp.key())
				r = i;
			if(k == temp.key())
				return temp.value();
			if(k > temp.key())
				l = i;
		}
		return NULL;
	}
	int size()
	{
		return list->length();
	}
};







### C++ STL `pair` 实现细节 #### 类定义与成员变量 `std::pair` 是标准模板库 (STL) 提供的一种用于存储两个不同类型数据的结构体。该类模板通常被用来作为其他容器(如 map 或 multimap)中的键值对[^1]。 ```cpp template <class T1, class T2> struct pair { typedef T1 first_type; typedef T2 second_type; T1 first; T2 second; // Constructors... }; ``` 此代码片段展示了最基础本的 `pair` 定义,其中包含了两个公有类型的别名 (`first_type`, `second_type`) 和两个公开的数据成员 (`first`, `second`) 来保存实际的对象实例。 #### 构造函数 为了方便创建不同需求下的配对组合,`pair` 支持多种构造方式: - 默认构造函数会分别使用默认构造的方式初始化 `first` 和 `second`. - 可以传入具体的初始值来显式设置这两个字段. - 还存在一个复制/移动构造函数以便于从已有的另一个 `pair` 对象快速构建新对象. ```cpp // Default constructor initializes both members using their default constructors constexpr pair() : first(), second() {} // Constructor that allows initializing with specific values for 'first' and 'second' constexpr pair(const T1& a, const T2& b) noexcept(/* see below */) : first(a), second(b) {} // Copy/move constructor from another pair object constexpr pair(const pair&) = default; constexpr pair(pair&&) = default; ``` 这些构造方法使得 `pair` 成为了一种非常灵活且易于使用的工具,在许多场景下都能发挥重要作用。 #### 操作符重载 除了基本属性外,`pair` 还实现了若干操作符重载功能,比如比较运算符 (< , > , == 等),这有助于简化逻辑判断语句并提高程序可读性: ```cpp friend constexpr bool operator==(const pair&, const pair&); friend constexpr bool operator<(const pair&, const pair&); // Other relational operators are also provided similarly... ``` 上述声明允许直接利用关系表达式来进行成对比测试而无需额外编写辅助函数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值