shared_ptr使用,构建list
2013-09-10 16:01
232 查看
节点类:
list类:
测试:
/** link list node **/ #ifndef NODE__H__ #define NODE__H__ #include <memory> template<typename T> class node { public: T val; std::shared_ptr<node> next; public: node() { next.reset(); } explicit node(const T& t) { val=t; next.reset(); } }; #endif
list类:
#ifndef list_h__ #define list_h__ #include "node.h" #include <memory> template<typename T> class list { public: list() { head.reset(); tail.reset(); node_null.reset(); } void push_back(const T& val) { if(head==node_null) { head=std::shared_ptr<node<T> >(new node<T>(val)); tail=head; } else { tail->next=std::shared_ptr<node<T> >(new node<T>(val)); tail=tail->next; } } T front() { return *head; } T back() { return *tail; } std::shared_ptr<node<T> >begin() { return head; } private: std::shared_ptr<node<T> > head; std::shared_ptr<node<T> > tail; public: std::shared_ptr<node<T> > node_null; }; #endif
测试:
#include "list.h" #include "node.h" #include <iostream> int main(int argc,char* argv[]) { list<int> l; l.push_back(23); l.push_back(2); auto t=l.begin(); while(t!=l.node_null) { std::cout<<t->val<<std::endl; t=t->next; } system("PAUSE"); return 0; }
相关文章推荐
- 小测试:boost之shared_ptr在容器类std::list中的使用
- 为什么使用enable_shared_from_this——shared_ptr两类错误
- auto_ptr,shared_ptr 智能指针的使用
- [C++11]_[初级]_[shared_ptr的使用场景]
- shared_ptr 的使用及注意事项
- shared_ptr的资料及与删除器使用遇到的问题
- C++智能指针:TR1的 shared_ptr 和 weak_ptr 使用介绍
- boost::shared_ptr使用技巧
- 非动态生成对象使用shared_ptr
- c++ shared_ptr智能指针使用注意事项
- c++11 条款19:使用std::shared_ptr来进行共享所有权的资源管理
- boost::shared_ptr 智能指针在项目中的使用
- boost.shared_ptr源码整理和使用说明
- shared_ptr和weak_ptr智能指针结合使用的一个实例
- C++:智能指针-TR1的shared_ptr和weak_ptr使用介绍
- [C++]Shared_ptr使用详解&&实现链表
- C++11中shared_ptr的使用
- Item 19: 使用srd::shared_ptr来管理共享所有权的资源
- utilities(C++)——单例(Singleton) (使用智能指针 shared_ptr)
- C++学习 std::tr1::shared_ptr使用的一点体会tr1库介绍