( ).
Skip iterators. Here is an example of implementation and use:
template <typename Iter>
void function(Iter begin, Iter end)
{
for (Iter it = begin; it != end; ++it)
{
std::cout << *it << std::endl;
}
}
int main()
{
std::string array[] = {"hello", "array", "world"};
function(array, array + 3);
std::vector<std::string> vec = {"hello", "vector", "world"};
function(vec.begin(), vec.end());
}
Please note that in many cases you really do not need to write this function, but you can build it using the library tools, and then simply apply to it std::for_each. Or better yet, use an existing algorithm, such as std::accumulateor std::find_if.
source
share