I was reading this question:
Specialize function for map like containers
And I tried to modify a little of 40two's answer:
namespace extract
{
template <typename T1, typename T2>
const T2& e(const std::pair<T1, T2>& r)
{
return r.second;
}
template <typename T>
const T& e(const T& r)
{
return r;
}
}
template<typename Iterator>
void print(Iterator begin, Iterator end)
{
while (begin != end)
{
std::cout << extract::e(*begin) << std::endl;
++begin;
}
}
calling it like this, works fine:
std::vector<int> v(3,1);
std::map<int, int> m;
m[0]=10;
m[1]=11;
m[2]=12;
print(v.begin(), v.end());
print(m.begin(), m.end());
What I am wondering is, how to achieve same thing with only passing begin
but not *begin
?
I would like to modify current e()
functions to let them directly overload for different types of iterators.
i.e. to call it like:
template<typename Iterator>
void print(Iterator begin, Iterator end)
{
while (begin != end)
{
// NOTE: only pass in begin, please modify e() to let it directly take iterator
std::cout << extract::e(begin) << std::endl;
++begin;
}
}
I think this would suit original question's need more, but I tried many ways and was not able to do it.
Thanks.