标签:empty += 类模板 兼容 ash operator div 种类 ras
template<typename T> class list; template<typename T> struct list_node { typedef T value_type; typedef T& reference_type; typedef const T const_reference_type; T value; list_node *prev; list_node *next; list_node(T const &value, list_node *prev, list_node *next) : value(value), prev(prev), next(next){} }; template<typename N> class list_iterator { N *pos; template<typename T> friend class list; public: typedef typename N::value_type value_type; typedef typename N::reference_type reference_type; typedef typename N::const_reference_type const_reference_type; typedef list_iterator<N> self_type; list_iterator() :pos(0){} list_iterator(N *pos) :pos(pos){} bool operator != (self_type const &right) const{ return pos != right.pos; } bool operator == (self_type const &right) const{ return pos != right.pos; } self_type& operator++(){ if (pos) pos = pos->next; return *this; } reference_type operator * () throw (std::runtime_error){ if (pos) return pos->value; else throw (std::runtime_error("null iterator!\n")); } }; template<typename T> class list { typedef list_node<T> node_type; node_type *head; public: typedef T value_type; typedef list_iterator<node_type> iterator; list() :head(){} ~list(){ while (head) { node_type *n = head; head = head->next; delete n; } } void push_front(T const &v) { head = new node_type(v, 0, head); if (head->next) { head->next->prev = head; } } void pop_front(T const &v) { if (head) { node_type *n = head; head = head->next; head->prev = 0; delete n; } } void insert(iterator it, T const &v) { node_type *n = it.pos; if (n) { node_type *new_node = new node_type(v, n, n->next); new_node->next->prev = new_node; n->next = new_node; } } void erase(iterator &it) { node_type *n = it.pos; ++it; if (n) { if (n->next) { n->next->prev = n->prev; } if (n->prev) { n->prev->next = n->next; } if (head == n) { head = n->next; } delete n; } } bool is_empty() const { return head == 0; } iterator begin(){ return iterator(head); } iterator end(){ return iterator(); } };
template<typename T> struct tree_node { typedef T value_type; typedef T& reference_type; typedef const T& const_reference_type; T value; tree_node *parent; tree_node *left; tree_node *right; tree_node(T const &value, tree_node *parent, tree_node *left, tree_node *right): value(value), parent(parent), left(left), right(right){} ~tree_node() { if (left) delete left; if (right) delete right; } }; template<typename N> class tree_iterator { const N *pos; public: typedef typename N::value_type value_type; typedef typename N::const_reference_type const_reference_type; typedef tree_iterator<N> self_type; tree_iterator() :pos(0){} tree_iterator(const N *pos) :pos(pos){} bool operator == (self_type const &right) const{ return pos == right.pos; } self_type& operator ++ (){ if (pos){ if (pos->right){ pos = pos->right; while (pos->left) { pos = pos->left; } } else { while (pos->parent && (pos->parent->right == pos)) { pos = pos->parent; } pos = pos->parent; } } return *this; } const_reference_type operator * () const throw(std::runtime_error) { if (pos) { return pos->value; } else { throw std::runtime_error("Null iterator!\n"); } } }; template<typename T> class set { typedef tree_node<T> node_type; node_type *root; public: typedef T value_type; typedef tree_iterator<node_type> const_iterator; set() :root(){} ~set(){ if (root) delete root; } bool insert(T const &v) { node_type **n = &root; node_type *p = 0; while (*n) { if (v == (*n)->value) { return false; } else { p = *n; n = v < (*n)->value ? &((*n)->left) : &((*n)->right); } } *n = new node_type(v, p, 0, 0); return true; } bool has(T const &v) { node_type *n = root; while (n) { if (v == n->value) return true; n = v < n->value ? n->left : n->right; } return false; } bool is_empty() const { return root == 0; } const_iterator begin() const{ node_type *n = root; while (n->left) n = n->left; return const_iterator(n); } const_iterator end() const { return const_iterator(); } };
template<typename C> typename C::value_type sum(C &c) { typedef typename C::value_type value_type; typedef typename C::iterator iterator; value_type sum(0); for (iterator i = c.begin(); i != c.end(); ++i) { sum += *i; } return sum; }
template<typename I> typename I::value_type sum(I begin, I end) { typedef typename I::value_type value_type; value_type sum(0); for (; begin != end; ++begin) { sum += *begin; } return sum; }
template<typename I> struct iterator_traits { typedef typename I::value_type value_type; }; template<typename P> struct iterator_traits<P*> { typedef P value_type; };
template<typename I> typename iterator_traits<I>::value_type sum(I begin, I end) { typedef typename iterator_traits<I>::value_type value_type; value_type sum(0); for (; begin != end; ++begin) { sum += *begin; } return sum; }
标签:empty += 类模板 兼容 ash operator div 种类 ras
原文地址:https://www.cnblogs.com/predator-wang/p/11509831.html