Dear all,
I have the following template function which prints to cout:
template <typename T> void prn_vec(const std::vector < T >&arg, string sep="")
{
for (unsigned n = 0; n < arg.size(); n++) {
cout << arg[n] << sep;
}
return;
}
// Usage:
//prn_vec<int>(myVec,"\t");
// I tried this but it fails:
/*
template <typename T> void prn_vec_os(const std::vector < T >&arg,
string sep="",ofstream fn)
{
for (unsigned n = 0; n < arg.size(); n++) {
fn << arg[n] << sep;
}
return;
}
*/
How can I modify it so that it also takes file handle as input and print out to that file as referred by the filehandle?
So that we can do something like:
#include <fstream>
#include <vector>
#include <iostream>
int main () {
vector <int> MyVec;
MyVec.push_back(123);
MyVec.push_back(10);
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
// prn_vec(MyVec,myfile,"\t");
myfile.close();
return 0;
}