Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <list>
- #include <iostream>
- #include <cassert>
- using namespace std;
- //both foo() and bar() try to do the same thing:
- //print the first elemnt in c and append it to c.
- template <typename Container>
- void foo(Container &c)
- {
- assert( !c.empty() );
- auto &head = *(begin(c));
- c.push_back(head);
- cout << head << endl;
- }
- template <typename Container>
- void bar(Container &c)
- {
- assert( !c.empty() );
- auto head = begin(c);
- c.push_back(*head);
- cout << *head << endl;
- }
- int main(int argc, const char *argv[])
- {
- {//1
- vector<int> c{1, 2, 3, 4};
- foo(c);
- }
- {//2
- vector<int> c{1, 2, 3, 4};
- bar(c);
- }
- {//3
- list<int> c{1, 2, 3, 4};
- foo(c);
- }
- {//4
- list<int> c{1, 2, 3, 4};
- bar(c);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement