Advertisement
Guest User

Untitled

a guest
Feb 26th, 2015
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <forward_list>
  3.  
  4. using namespace std;
  5.  
  6. int main(void)
  7. {
  8. forward_list<int> list1 = {1, 2, 3, 4, 5, 6};
  9. forward_list<int> list2;
  10.  
  11. //Transfer elements between 2 and 5 from list1 to list2.
  12. forward_list<int>::const_iterator iter_before = list1.before_begin();
  13. forward_list<int>::const_iterator iter = list1.begin();
  14. while ( iter != list1.end())
  15. {
  16. int item = *iter;
  17. cout << item ;
  18. if (2 <= item && item <= 5 )
  19. {
  20. list2.splice_after(list2.before_begin(),list1,iter_before);
  21. iter_before = iter;
  22. iter++;
  23. cout << "! "; // Indicates if current item is transferred.
  24. }
  25. else
  26. {
  27. iter++;
  28. iter_before++;
  29. cout << " ";
  30. }
  31. }
  32.  
  33. cout << "nList 1: ";
  34. for (auto item : list1)
  35. {
  36. cout << item << " ";
  37. }
  38. cout << "nList 2: ";
  39. for (auto item : list2)
  40. {
  41. cout << item << " ";
  42. }
  43. cout << endl;
  44.  
  45. return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement