list.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // list.cpp -- using a list
  2. #include <iostream>
  3. #include <list>
  4. #include <iterator>
  5. #include <algorithm>
  6. void outint(int n) {std::cout << n << " ";}
  7. int main()
  8. {
  9. using namespace std;
  10. list<int> one(5, 2); // list of 5 2s
  11. int stuff[5] = {1,2,4,8, 6};
  12. list<int> two;
  13. two.insert(two.begin(),stuff, stuff + 5 );
  14. int more[6] = {6, 4, 2, 4, 6, 5};
  15. list<int> three(two);
  16. three.insert(three.end(), more, more + 6);
  17. cout << "List one: ";
  18. for_each(one.begin(),one.end(), outint);
  19. cout << endl << "List two: ";
  20. for_each(two.begin(), two.end(), outint);
  21. cout << endl << "List three: ";
  22. for_each(three.begin(), three.end(), outint);
  23. three.remove(2);
  24. cout << endl << "List three minus 2s: ";
  25. for_each(three.begin(), three.end(), outint);
  26. three.splice(three.begin(), one);
  27. cout << endl << "List three after splice: ";
  28. for_each(three.begin(), three.end(), outint);
  29. cout << endl << "List one: ";
  30. for_each(one.begin(), one.end(), outint);
  31. three.unique();
  32. cout << endl << "List three after unique: ";
  33. for_each(three.begin(), three.end(), outint);
  34. three.sort();
  35. three.unique();
  36. cout << endl << "List three after sort & unique: ";
  37. for_each(three.begin(), three.end(), outint);
  38. two.sort();
  39. three.merge(two);
  40. cout << endl << "Sorted two merged into three: ";
  41. for_each(three.begin(), three.end(), outint);
  42. cout << endl;
  43. // cin.get();
  44. return 0;
  45. }