+ 1
Ostream c++
How can I have write the function void printData(ostream &os, ...), which writes array's contents in a file or prints out on the console?
1 Respuesta
+ 4
Certainly doable if you know the array size at compile time:
const int SIZE = 100;
template <typename T>
void printData(std::ostream &os, T array[SIZE])
{
os.rdbuf(std::cout.rdbuf());
for (int i = 0; i < SIZE; os << array[i++]);
}
// I would suggest std::vector<> as a replacement for flexibility.
template <typename T>
void printData(std::ostream &os, std::vector<T> arr)
{
os.rdbuf(std::cout.rdbuf());
for (T i : arr) os << i;
}