CHAPTER8-42.cpp
上传用户:fjc899
上传日期:2007-07-03
资源大小:187k
文件大小:4k
源码类别:

STL

开发平台:

C/C++

  1. //文件名:CHAPTER8-42.cpp
  2. #include <list>
  3. #include <iostream>
  4. #include <functional>
  5. int main()
  6. {
  7. //default constructor
  8. std::list<int> c1 ;
  9. //create list with 10 copies of 4
  10. std::list<int> c2(10, 4) ;
  11. //copy constructor
  12. std::list<int> c3(c2) ;
  13. int ai[] = {0, 1, 2, 3, 4, 5} ;
  14. int i ;
  15. std::list<int> c4 ;
  16. //get_allocator
  17. std::list<int>::allocator_type a1 = c4.get_allocator() ;
  18. //push_back
  19. for(i = 0; i < 5; i++)
  20. c4.push_back(ai[i]) ;
  21. //range copy constructor
  22. std::list<int> c5(c4.begin(), c4.end()) ;
  23. //begin, end
  24. std::cout << "c4 (using begin, end) = " ;
  25. std::list<int>::iterator Iter ;
  26. for(Iter = c4.begin(); Iter != c4.end(); Iter++)
  27. std::cout << *Iter << ", " ;
  28. std::cout << std::endl ;
  29. //rbegin, rend
  30. std::cout << "c4 (using rbegin, rend) = " ;
  31. std::list<int>::reverse_iterator RevIter ;
  32. for(RevIter = c4.rbegin(); RevIter != c4.rend(); RevIter++)
  33. std::cout << *RevIter << ", " ;
  34. std::cout << std::endl ;
  35. //assign
  36. c2.assign(c5.begin(), c5.end()) ;
  37. c1.assign(10, 4) ;
  38. //back
  39. std::cout << "last element in c2 = " << c2.back() << std::endl ;
  40. //front
  41. std::cout << "first element in c2 = " << c2.front() << std::endl ;
  42. //size
  43. std::cout << "number of elements in c2 = " << c2.size() << std::endl ;
  44. //max_size
  45. std::cout << "max number of elements c2 can hold using current allocator = " 
  46. << c2.max_size() << std::endl ;
  47. //erase
  48. c3.erase(c3.begin(), c3.end()) ;
  49. //clear
  50. c2.clear() ;
  51. //empty
  52. if (c2.empty() == true)
  53. std::cout << "c2 is now empty" << std::endl ;
  54. //resize
  55. c2.resize(10, 30) ;
  56. std::cout << "number of elements in c2 = " << c2.size() << std::endl ;
  57. std::cout << "last element in c2 = " << c2.back() << std::endl ;
  58. std::cout << "first element in c2 = " << c2.front() << std::endl ;
  59. //push_front
  60. c2.push_front(25) ;
  61. std::cout << "first element in c2 = " << c2.front() << std::endl ;
  62. //pop_back
  63. c2.pop_back() ;
  64. std::cout << "last element in c2 = " << c2.back() << std::endl ;
  65. //pop_front
  66. c2.pop_front() ;
  67. std::cout << "first element in c2 = " << c2.front() << std::endl ;
  68. //swap
  69. c3.swap(c2) ;
  70. std::cout << "number of elements in c3 = " << c3.size() << std::endl ;
  71. std::cout << "last element in c3 = " << c3.back() << std::endl ;
  72. std::cout << "first element in c3 = " << c3.front() << std::endl ;
  73. //insert
  74. c1.insert(c1.begin(), 20) ;
  75. c3.insert(c3.begin(), 4, 10) ;
  76. c3.insert(c3.end(), c5.begin(), c5.end()) ;
  77. std::cout << "c1 = " ;
  78. for(Iter = c1.begin(); Iter != c1.end(); Iter++)
  79. std::cout << *Iter << ", " ;
  80. std::cout << std::endl ;
  81. //reverse
  82. c3.reverse() ;
  83. std::cout << "c3, after reversing = " ;
  84. for(Iter = c3.begin(); Iter != c3.end(); Iter++)
  85. std::cout << *Iter << ", " ;
  86. std::cout << std::endl ;
  87. //remove
  88. c3.remove(30) ;
  89. std::cout << "c3, after removing all occurences of 30 = " ;
  90. for(Iter = c3.begin(); Iter != c3.end(); Iter++)
  91. std::cout << *Iter << ", " ;
  92. std::cout << std::endl ;
  93. c2.insert(c2.begin(), 4, 10) ;
  94. //remove_if
  95. c2.remove_if(std::bind2nd(std::not_equal_to<int>(), 10)) ;
  96. std::cout << "c2, after removing all elements which are not equal to 10 = " ;
  97. for(Iter = c2.begin(); Iter != c2.end(); Iter++)
  98. std::cout << *Iter << ", " ;
  99. std::cout << std::endl ;
  100. c2.insert(c2.begin(), 35) ;
  101. //sort
  102. c3.sort() ;
  103. std::cout << "c3, after sorting = " ;
  104. for(Iter = c3.begin(); Iter != c3.end(); Iter++)
  105. std::cout << *Iter << ", " ;
  106. std::cout << std::endl ;
  107. //unique
  108. c2.unique() ;
  109. std::cout << "c2, after removing duplicates = " ;
  110. for(Iter = c2.begin(); Iter != c2.end(); Iter++)
  111. std::cout << *Iter << ", " ;
  112. std::cout << std::endl ;
  113. //splice
  114. c3.splice(c3.end(), c2) ;
  115. c3.splice(c3.end(), c4, c4.begin()) ;
  116. c3.splice(c3.end(), c1, c1.begin(), c1.end()) ;
  117. c3.sort() ;
  118. c3.unique() ;
  119. std::cout << "c3 = " ;
  120. for(Iter = c3.begin(); Iter != c3.end(); Iter++)
  121. std::cout << *Iter << ", " ;
  122. std::cout << std::endl ;
  123. std::cout << "c2 = " ;
  124. for(Iter = c2.begin(); Iter != c2.end(); Iter++)
  125. std::cout << *Iter << ", " ;
  126. std::cout << std::endl ;
  127. std::cout << "c1 = " ;
  128. for(Iter = c1.begin(); Iter != c1.end(); Iter++)
  129. std::cout << *Iter << ", " ;
  130. std::cout << std::endl ;
  131. return 0 ;
  132. }