Which way is the right way?

I'm passing a vector or integers to a print function. I have an example from a book that does it this way.
1
2
3
4
5
6
7
8
9
10
// prototype for function template printVector
template < typename T > void printVector( const vector< T > &integers2 );

template < typename T > void printVector( const vector< T > &integers2 )
{
typename vector< T >::const_iterator constIterator; // const_iterator
// display vector elements using const_iterator
for ( constIterator = integers2.begin(); constIterator != integers2.end(); ++constIterator )
cout << *constIterator << ' ';
} // end function printVector 


Wrote mine this way

1
2
3
4
5
6
7
8
9
10
11
12
//function prototype
void print(const vector <int>&);

//function
void print (const vector<int> &vectorToPrint)
{
    vector<int>::const_iterator constIterator;
    for (constIterator = vectorToPrint.begin(); constIterator != vectorToPrint.end(); ++constIterator)
    {
        cout << *constIterator << " ";
    }


the example from the book is using integers as well. Is there a reason it does it that way, by creating a template for it. Don't you only have to do that if the data type may be different? Both ways work just fine so i'm not sure if there is some underlying issuer i'm missing here.
The general approach is always better than particular approach. Let assume that you also need to output an array of unsigned integers or of long integes what will you do? Write three different functions?
Ya, I understand the reason behind the general approach. But if I declare my vector like this vector<int> possiblePalindrome; there really isn't any question about the type so i would be safe in not creating the template print function. Right? Or am i wrong.
You safe nothing but spend a time to write a particular function instaed of write a code which could be used in many projects.
gotcha Thanks for the help.
Topic archived. No new replies allowed.