Depending on the level of abstraction you want to provide, you have to choose between Olaf's approach or what STL uses at a high level:
template <class Iter>
void function(Iter first, Iter last)
{
std::size_t size = std::distance(first, last);
for ( Iter it = first; it != last; ++it )
{
std::cout << *it << std::endl;
}
}
Thus, you can use this function not only for arrays, but also for various types of STLs: vectors, lists, queues, stacks, etc.
int tab[4] = {1,2,3,4};
function(tab, tab+4);
std::vector<int> vec;
function(vec.begin(), vec.end());
source
share