Advertisement
Guest User

Untitled

a guest
Jul 28th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. #include <vector>
  2. #include <list>
  3. #include <iostream>
  4. #include <cassert>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. //both foo() and bar() try to do the same thing:
  10. //print the first elemnt in c and append it to c.
  11.  
  12.  
  13. template <typename Container>
  14. void foo(Container &c)
  15. {
  16. assert( !c.empty() );
  17. auto &head = *(begin(c));
  18. c.push_back(head);
  19. cout << head << endl;
  20. }
  21.  
  22. template <typename Container>
  23. void bar(Container &c)
  24. {
  25. assert( !c.empty() );
  26. auto head = begin(c);
  27. c.push_back(*head);
  28. cout << *head << endl;
  29. }
  30.  
  31. int main(int argc, const char *argv[])
  32. {
  33. {//1
  34. vector<int> c{1, 2, 3, 4};
  35. foo(c);
  36. }
  37.  
  38. {//2
  39. vector<int> c{1, 2, 3, 4};
  40. bar(c);
  41. }
  42.  
  43. {//3
  44. list<int> c{1, 2, 3, 4};
  45. foo(c);
  46. }
  47.  
  48. {//4
  49. list<int> c{1, 2, 3, 4};
  50. bar(c);
  51. }
  52. return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement