std::set和std::multiset
std::set
template <typename Tp> struct Identity_ { const Tp& operator()(const Tp& x) const { return x; } }; template <typename Key, typename Compare = std::less<Key>> class Myset { public: typedef Key key_type; typedef Key value_type; typedef Compare key_compare; typedef Compare value_compare; private: typedef Rb_tree<key_type, value_type, Identity_<value_type>, key_compare> Rep_type; Rep_type m_rbtree; // red-black tree representing set public: typedef typename Rep_type::iterator iterator; typedef typename Rep_type::size_type size_type; Myset() : m_rbtree() {} Myset(Myset&& x) : m_rbtree(std::move(x.m_rbtree)) {} iterator begin() const { return m_rbtree.begin(); } iterator end() const { return m_rbtree.end(); } bool empty() const { return m_rbtree.empty(); } size_type size() const { return m_rbtree.size(); } void swap(Myset& x) { m_rbtree.swap(x.m_rbtree); } std::pair<iterator, bool> insert(const value_type& x) { return m_rbtree.insert_unique(x); } void erase(iterator position) { m_rbtree.erase(position); } size_type erase(const key_type& x) { return m_rbtree.erase(x); } void clear() { m_rbtree.clear(); } iterator find(const key_type& x) { return m_rbtree.find(x); } size_type count(const key_type& x) const { return m_rbtree.count(x); } };
std::multiset
template <typename Key, typename Compare = std::less<Key>> class Mymultiset { public: typedef Key key_type; typedef Key value_type; typedef Compare key_compare; typedef Compare value_compare; private: typedef Rb_tree<key_type, value_type, Identity_<value_type>, key_compare> Rep_type; Rep_type m_rbtree; public: typedef typename Rep_type::iterator iterator; typedef typename Rep_type::size_type size_type; Mymultiset() : m_rbtree() {} Mymultiset(Mymultiset&& x) : m_rbtree(std::move(x.m_rbtree)) {} iterator begin() const { return m_rbtree.begin(); } iterator end() const { return m_rbtree.end(); } bool empty() const { return m_rbtree.empty(); } size_type size() const { return m_rbtree.size(); } void swap(Mymultiset& x) { m_rbtree.swap(x.m_rbtree); } iterator insert(const value_type& x) { return m_rbtree.insert_equal(x); } void erase(iterator position) { m_rbtree.erase(position); } size_type erase(const key_type& x) { return m_rbtree.erase(x); } void clear() { m_rbtree.clear(); } iterator find(const key_type& x) { return m_rbtree.find(x); } size_type count(const key_type& x) const { return m_rbtree.count(x); } };