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 26 27 28 29 30 31 32 33 34 35 36
|
#include <iostream>
#include <iterator>
#include <vector>
#include <string>
#include <list>
template < typename ITERATOR > void print( ITERATOR begin, ITERATOR end )
{
if( begin != end ) // if the range is not empty
{
std::cout << *begin << ' ' ; // print the first element
print( ++begin, end ) ; // print the remaining elements (recursive call)
}
else std::cout << '\n' ; // end of sequence; print a new line (recursion 'bottoms out')
}
template < typename SEQUENCE > void print( const SEQUENCE& seq )
{
// https://en.cppreference.com/w/cpp/iterator/begin
print( std::begin(seq), std::end(seq) ) ;
}
int main()
{
const int a[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;
print(a) ;
const std::vector<std::string> b { "zero", "one", "two", "three", "four", "five" } ;
print(b) ;
const std::list<double> c { 0.1, 2.3, 4.5, 6.7, 8.9 } ;
print(c) ;
const std::string d = "abcdefghijkl" ;
print(d) ;
}
|