C++模板实战5: 迭代器与容器(二)

2014-11-24 07:27:27 · 作者: · 浏览: 1
llptr){} ~Set(){ if(root) delete root; } bool insert(T const& v){ node_type** n=&root; node_type* p=nullptr; 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,nullptr,nullptr); return true; } bool has(T const& v){ node_type* n=root; while(n){ if(v==n->value) return true; n=v value n->left:n->right; } return false; } bool empty() const{ return root==nullptr; } const_iterator begin(){ node_type* n=root; while(n->left) n=n->left; return const_iterator(n); } const_iterator end() const{ return const_iterator(); } }; int main(){ Set mySet; int x=10; mySet.insert(x); cout<