+ 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?

17th Feb 2018, 8:26 PM
Winter Soldier
Winter Soldier - avatar
1 Answer
+ 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; }
18th Feb 2018, 2:39 AM
Hatsy Rei
Hatsy Rei - avatar