doc_how_to_use.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2006-2013
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // See http://www.boost.org/libs/intrusive for documentation.
  10. //
  11. /////////////////////////////////////////////////////////////////////////////
  12. //[doc_how_to_use_code
  13. #include <boost/intrusive/list.hpp>
  14. #include <vector>
  15. using namespace boost::intrusive;
  16. class MyClass : public list_base_hook<>
  17. {
  18. int int_;
  19. public:
  20. list_member_hook<> member_hook_;
  21. MyClass(int i) : int_(i) {}
  22. //<-
  23. int get_int() const { return int_; }
  24. //->
  25. };
  26. //Define a list that will store MyClass using the base hook
  27. typedef list<MyClass> BaseList;
  28. //Define a list that will store MyClass using the member hook
  29. typedef member_hook
  30. < MyClass, list_member_hook<>, &MyClass::member_hook_> MemberOption;
  31. typedef list<MyClass, MemberOption> MemberList;
  32. int main()
  33. {
  34. typedef std::vector<MyClass>::iterator VectIt;
  35. //Create several MyClass objects, each one with a different value
  36. std::vector<MyClass> values;
  37. for(int i = 0; i < 100; ++i) values.push_back(MyClass(i));
  38. BaseList baselist;
  39. MemberList memberlist;
  40. //Now insert them in the reverse order in the base hook list
  41. for(VectIt it(values.begin()), itend(values.end())
  42. ; it != itend ; ++it){
  43. baselist.push_front(*it);
  44. }
  45. //Now insert them in the same order as in vector in the member hook list
  46. for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it)
  47. memberlist.push_back(*it);
  48. //Now test lists
  49. {
  50. BaseList::reverse_iterator rbit(baselist.rbegin());
  51. MemberList::iterator mit(memberlist.begin());
  52. VectIt it(values.begin()), itend(values.end());
  53. //Test the objects inserted in the base hook list
  54. for(; it != itend; ++it, ++rbit)
  55. if(&*rbit != &*it) return 1;
  56. //Test the objects inserted in the member hook list
  57. for(it = values.begin(); it != itend; ++it, ++mit)
  58. if(&*mit != &*it) return 1;
  59. }
  60. return 0;
  61. }
  62. //]