Standard output function template

I am trying to code a function that takes in a range of values and prints them using std::cout. This is the prototype

1
2
template < typename T >
void Output(const T* first, const T* last, char outForChar= '\0')


This function is supposed to output the data in the range [first, last). It first checks if the passed inc outForChar is '\0', if not then there should be no separation in between each piece of data. If so, then it will be outputted with the outForChar before each piece of data. I am trying to figure out how to start on this, and wanted to know how I can approach this? This is my work so far, and I know it is not correct by any means so I would really appreciate any pointers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  T* i;
  if (outForChar == '\0')
  {
    for (i = first; i != last; ++i)
    {
      std::cout << i;
    }
  }
  else
  {
    for (i = first; i != last; ++i)
    {
      std::cout << outForChar << i;
    }
  }
Last edited on
Yours is close to:
http://www.cplusplus.com/reference/iterator/ostream_iterator/ostream_iterator/
... except yours puts delimiter before the value, is limited to pointers, single-char delimiter, and is hardcoded to std::cout.

Then there is:
http://www.cplusplus.com/reference/algorithm/for_each/
std::for_each( first, last, [](const auto & y){std::cout << "xXx" << y;} );


... appreciate any pointers.

What do you print out now? Pointers. Should you print values that are pointed to by the pointers?
Last edited on
I have come up with this code: https://pastebin.com/jzskP0jf

Does it look good? I also tried the const auto way but for some reason my compiler is having an issue with the lambda function so I went with another way.
What is your compiler? Older GCC defaults to older C++, but can be told to enable a more recent C++ standard.
Topic archived. No new replies allowed.