STL项目实践

编写一个STL容器

  • 项目需求:

    • 实现一个通用的容器,能够支持插入多种不同的普通类型(包含 int char float double 等)和自定义结构体和自定义类的对象,并能根据每种不同类型的比较规则从容器中取得最大或最小的那个值或对象。

  • 接口设计

    • 构造函数
    • 赋值运算符重载
    • 大小
    • 插入和删除
    • 查找最大值和最小值
  • 头文件:

    • sizeFilter.hpp

    • #ifndef _SIZEFILTER_
      #define _SIZEFILTER_
      
      #include <iostream>
      #include <set>
      
      using namespace std;
      
      template<class _Ty,
      	class _Container = set<_Ty> >
      class sizeFilter {
      
      	typedef sizeFilter<_Ty, _Container> _Myt;
      	typedef typename _Container::size_type size_type;
      	typedef typename _Container::value_type value_type;
      public:
      	sizeFilter() : c() {
      		//默认构造函数,初始化内置容器
      
      	}
      	sizeFilter(const _Myt& _Right) : c(_Right.c) {
      		//构造函数,通过指定的sizeFilter容器构造
      	}
      	sizeFilter(const _Container& _Cont) :c(_Cont) {
      		//构造函数,通过指定的容易构造
      	}
      	_Myt& operator = (const _Myt& _Right) { // A = B = C = D
      		//使用另一个sizeFilter赋值
      		c = _Right.c;
      		return (*this);
      	}
      	bool empty() const {
      		//测试 SizeFilter是否为空格
      		return (c.empty());
      	}
      	//typename _Container::size_type
      	size_type size() const {
      		return (c.size());
      	}
      	bool  insert(const value_type& _Val) {
      		/*
      		pair<_Container::iterator, bool> ret = c.insert(_Val);
      		if (ret.second) {
      			cout << "插入" << _Val << " 成功! " << endl;
      			return true;
      		}
      		*/
              //为什么不适用pair? 因为迭代器能更加通用
      		typename _Container::iterator ci = c.insert(c.begin(),_Val);
      		if (ci != c.end()) {
      			cout << "插入" << _Val << " 成功! " << endl;
      			return true;
      		}
      		cout << "插入" << _Val << " 失败! " << endl;
      		return false;
      	}
      	bool erase(const value_type& _Val) {
      		//擦除元素
      		if (c.erase(_Val) > 0) {
      
      			return true;
      		}
      
      		return false;
      	}
      	pair<value_type,bool> getMin() {
      		//获取最小值
      		std::pair<value_type, bool> ret;
      		typename _Container::iterator min = c.begin();
      		if (min != c.end()) {//set第一个就是最小值
      			ret.first = *min;
      			ret.second = true;//取到了
      			return ret;
      		}
      		ret.second = false;//没取到
      		return ret;
      	}
      	pair<value_type, bool> getMax() {
      		//获取最小值
      		std::pair<value_type, bool> ret;
      		typename _Container::iterator end = c.end();
      		if (c.size()>0) {//set最后一个就是最大值
      			ret.first = *(--end);
      			ret.second = true;//取到了
      			return ret;
      		}
      		ret.second = false;//没取到
      		return ret;
      	}
      protected:
      	_Container c; //set
      };
      
      #endif 
      
  • 测试文件

    • p15_stl_实践.cpp

    • #include <Windows.h>
      #include <iostream>
      #include "SizeFilter.hpp"
      
      using namespace std;
      
      int main(void) {
      	sizeFilter<int> sf;
      
      	sizeFilter<int, multiset<int>> smt;
      	smt.insert(11);
      
      	//smt.getMax();
      	//smt.getMax();
      	for (int i = 0; i < 10; i++) {
      		sf.insert(i * 5);
      	}
      	sf.erase(0);
      	sf.erase(45);
      	cout << "获取结果: " << endl;
      	//获取最大值
      	pair<int, bool> pb1 = sf.getMax();
      	if (pb1.second) {
      		cout << "max: " << pb1.first << endl;
      	}
      	else {
      		cout << "Not Found. " << endl;
      	}
      	//获取最小值
      	pair<int, bool> pb2 = sf.getMin();
      	if (pb2.second) {
      		cout << "min: " << pb2.first << endl;
      	}
      	else {
      		cout << "Not Found. " << endl;
      	}
      	system("pause");
      }
      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值