#include #include #include using namespace std; template void print_vec_index( const vector & v ) { cout << '<'; for (int sz = v.size(), i = 0; i < sz; ++i) cout << ' ' << v[i]; cout << " >"; } template void print_vec_iterator1( const vector & v ) { // Old C++ cout << '<'; // UNCOMMENT the different for loops to see the errors you get. // WRONG: Need typename before template type since vector doesn't exist!!! // for (vector::const_iterator i = begin(v); i != end(v); ++i) // WRONG: Need a constant iterator, as v is a const reference // for (typename vector::iterator i = begin(v); i != end(v); ++i) for (typename vector::const_iterator i = begin(v); i != end(v); ++i) cout << ' ' << *i; cout << " >"; } template void print_vec_iterator2( const vector & v ) { // New C++ (C++11) cout << '<'; for (auto i = begin(v); i != end(v); ++i) cout << ' ' << *i; cout << " >"; } int main() { vector vi; vi.push_back(1); vi.push_back(2); vi.push_back(3); print_vec_index(vi); cout << endl; print_vec_iterator1(vi); cout << endl; print_vec_iterator2(vi); cout << endl; return 0; }