Implement function templates

Need help implementing prototype. Data gives the data in the range [init, final) to standard output. If the output formatting character ofc is '\0' then the output data is not separated by anything. If ofc is any other character then ofc should be output preceeding each item in the range.

1
2
template <typename T>
void Data (const T* init, const T* final, char ofc = '\0');
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <iterator>
#include <string>

template < typename ITERATOR >
void dump_data( ITERATOR begin, ITERATOR end, char ofc = 0 )
{
    const char prefix[] = { ofc, '\0' } ;
    for( ; begin != end ; ++begin ) std::cout << prefix << *begin ;
    std::cout << '\n' ;
}

template < typename SEQUENCE >
void dump_data( const SEQUENCE& seq, char ofc = 0 )
{ dump_data( std::begin(seq), std::end(seq), ofc ) ; }

int main()
{
    const int a[] { 12, 34, 56, 78, 90, 98, 76, 54, 32 } ;
    dump_data(a) ; // 123456789098765432
    dump_data( a, '#' ) ; // #12#34#56#78#90#98#76#54#32

    const std::string str = "abracadabra" ;
    dump_data( str, '.' ) ; // .a.b.r.a.c.a.d.a.b.r.a
}

http://coliru.stacked-crooked.com/a/74b00d7a21c1f8b4
Can you explain it to me?
This loop iterates through the sequence (from begin, up to not equal to end) and prints every element to stdout.
for( ; begin != end ; ++begin ) std::cout << *begin ;

This null terminated c-style string const char prefix[] = { ofc, '\0' } ;
would be an empty string if ofc is the null character;
otherwise it would be a string containing the single non-null character ofc

In the body of the loop std::cout << prefix << *begin ;,
the prefix (nothing if ofc is the null character) is printed before each item.
Topic archived. No new replies allowed.