I was trying to print a vector<int>
using a helper function as follows:
This doesn't works -
template<class T>
void print(const std::vector<T>& v)
{
std::vector<T>::const_iterator i;
for (i = v.begin(); i != v.end(); i++)
std::cout << *i << " ";
std::cout << std::endl;
}
Edit: I get this.
But this works -
template<class T>
void print(const std::vector<T>& v)
{
// changed std::vector<T> to std::vector<int>
std::vector<int>::const_iterator i;
for (i = v.begin(); i != v.end(); i++)
std::cout << *i << " ";
std::cout << std::endl;
}
I wanted to ask following things :
- Why does first doesn't work and second does?
- What are alternative ways to write a function for same functionality? P.S. I don't want any of the element to be changed inside the function. I guess it can be done using for_each() algorithm. But I am not sure how I would write the predicate for it.