Suppose that we want to use iterators to loop over the elements of an std::unordered_map<T, U>. The iterators will point to elements of type std:: pair<const T, U>.
I know you can do it like this by using ::const_iterator (apologies for this code not being entirely of the cut-and-paste variety):
Code:
typedef std::tuple<int, int> TupleII;
std::unordered_map<Key<TupleII>, Sheet<TupleII>> input;
(... do something to fill in the values of "input"...)
for (typename std::unordered_map<Key <TupleII>,
Sheet<TupleII>>::const_iterator
s=input.cbegin(); s!=input.cend(); ++s)
{
printf("%s\n", ascii(s->second).c_str());
}
or like this, using range-based for loops (leaving out the first few lines from above):
Code:
for (const auto& s : input)
{
printf("%s\n", ascii(s.second).c_str());
}
Here "ascii" is just some function to print values to the screen.
My question is: Is there a way to explicitly declare the iterator "s" as an object of the iterator type of the class involved? What I mean is something along the lines of
Code:
for (const std::pair<const Key<TupleII>, Sheet<TupleII>>
s=input.cbegin(); s!=input.cend(); ++s)
{
printf("%s\n", ascii(s.second).c_str());
}
...but which would actually work. As coded here, it doesn't.
I know that the range-based "for" is the preferable option here. I just wondered whether this could be done.