• 从零开始写STL—set/map


    这一部分只要把搜索树中暴露的接口封装一下,做一些改动。

    set源码剖析

    	template<typename T>
    	class set
    	{
    	public:
    		typedef T key_type;
    		typedef BStree_iterator<T> iterator;
    	private:
    		BStree<T> sequence;
    	public:
    		set() :sequence() {}
    		iterator begin()
    		{
    			return sequence.begin();
    		}
    		iterator end()
    		{
    			return sequence.end();
    		}
    		void erase(iterator pos)
    		{
    			if (pos.p.node == NULL)
    				return;
    			sequence.erase(pos);
    		}
    		iterator find(const key_type& key)
    		{
    			return sequence.find(key);
    		}
    		size_t count(const key_type& x)
    		{
    			iterator pos = sequence.find(x);
    			if (pos.p.node == NULL)
    				return 0;
    			else
    				return 1;
    		}
    		size_t erase(const key_type& x)
    		{
    			iterator pos = sequence.find(x);
    			if (pos.p.node == NULL)
    				return 0;
    			else
    			{
    				erase(pos);
    				return 1;
    			}
    		}
    
    		bool empty()
    		{
    			return sequence.empty();
    		}
    		size_t size()
    		{
    			return sequence.size();
    		}
    		iterator insert(const T& val)
    		{
    			iterator f = sequence.find(val);
    			if (f == NULL)
    				return sequence.insert(val);
    			else
    				return f;
    		}
    		iterator lower_bound(const key_type& x)
    		{
    			return sequence.lower_bound(x);
    		}
    		iterator upper_bound(const key_type& x)
    		{
    			return sequence.upper_bound(x);
    		}
    	};
    

    map

    二叉搜索树中存储的元素—map_pair

    template<typename K, typename T>
    	struct map_pair
    	{
    		typedef map_pair<K, T> self;
    		K first;
    		T second;
    		operator pair<K, T>()
    		{
    			return ministl::make_pair<K, T>(first, second);
    		}
    		map_pair(const pair<K, T>& rhs)
    		{
    			first = rhs.first;
    			second = rhs.second;
    		}
    		map_pair(const K& key, const T& val)
    		{
    			first = key, second = val;
    		}
    		bool operator==(const self& rhs) const
    		{
    			return first == rhs.first;
    		}
    		bool operator!=(const self& rhs) const
    		{
    			return !(*this == rhs);
    		}
    		bool operator<(const self& rhs) const
    		{
    			return first < rhs.first;
    		}
    		bool operator>(const self& rhs) const
    		{
    			return first > rhs.first;
    		}
    		bool operator>=(const self& rhs) const
    		{
    			return !(first < rhs.first);
    		}
    		bool operator<=(const self& rhs) const
    		{
    			return !(first > rhs.first);
    		}
    	};
    

    map源码

    	template<typename K, typename T>
    	class map
    	{
    	private:
    		typedef map_pair<K, T> key_value;
    		typedef size_t size_type;
    		BStree<key_value> sequence;
    	public:
    		typedef BStree_iterator<key_value> iterator;
    		map() :sequence() {}
    		iterator begin()
    		{
    			return sequence.begin();
    		}
    		iterator end()
    		{
    			return sequence.end();
    		}
    		bool empty()
    		{
    			return sequence.empty();
    		}
    		size_type size()
    		{
    			return sequence.size();
    		}
    		iterator find(const K& x)
    		{
    			return sequence.find(map_pair<K, T>(x, T()));
    		}
    		size_type count(const K& x)
    		{
    			if (sequence.find(map_pair<K, T>(x, T())).p.node == NULL)
    				return 0;
    			else
    				return 1;
    		}
    		auto insert(const key_value& key)
    		{
    			return sequence.insert(map_pair<K,T>(key,T()));
    		}
    		template <class InputIterator>
    		void insert(InputIterator first, InputIterator last)
    		{
    			for (auto it = first; it != last; it++)
    				insert(*first);
    		}
    		void erase(const K& key)
    		{
    			return sequence.erase(map_pair<K, T>(key, T()));
    		}
    		iterator upper_bound(const K& key)
    		{
    			return sequence.upper_bound(map_pair<K, T>(key, T()));
    		}
    		iterator lower_bound(const K& key)
    		{
    			return sequence.lower_bound(map_pair<K, T>(key, T()));
    		}
    
    
    		T& operator [](const K& x)
    		{
    			pair<iterator,bool> res = sequence.insert(map_pair<K,T>(x,T()));
    			return (*res.first).second;
    		}
    
    	};
    
  • 相关阅读:
    递归算法变相考察面试题
    Flink Source 的几种方案
    nohup: 无法运行命令 ‘/bin/java‘: 没有那个文件或目录
    Flink StreamExecutionEnvironment 一些细节&Flink 执行模式
    Scala 开发环境配置
    Scala 基本使用
    Flink 基本转换算子传参方式的总结
    Kafka 概述
    Lambda expressions are not supported at language level '5'
    Flink 物理分区
  • 原文地址:https://www.cnblogs.com/joeylee97/p/8664818.html
Copyright © 2020-2023  润新知