Jim Lear wrote: <snip>
Not only might it be a design flaw, but it may invite inefficiency. But really it's just consistency with other languages which iterate over the keys (like awk)
As you acknowledge, this is inefficient. The keys and mapped values necessarily exist in pairs, so once you've found a key, you've found the mapped value itself. Why bother performing another lookup? C++ is intended to be efficient enough that there's no need for any lower-level language except the odd bit of assembly.
and code clarity. Someone unfamiliar with STL would find the following obvious: for (m_type::iterator i=m.begin();...) for (mi_type::iterator j=m[i].begin();...) cout << m[i][j] << endl; But they may find for (m_type::iterator i=m.begin();...) for (mi_type::iterator j=i->second.begin();...) cout << j->second << endl; obtuse.
But "STL" or rather the corresponding parts of the standard library are a fundamental part of standard C++. Someone who learns C++ should learn how to use these things, and someone who hasn't wouldn't understand either of those!
Perhaps folks intimately familiar with iterators may find this is hard to believe. :-) An alternative question might be, why should one who uses maps and vectors also be required to be familiar with the intricacies of iterators? To me, that seems like a design flaw. I have never encountered another language that requires the user to understand iterators to the extent STL does.
Iterators are what allow you to use generic algorithms on containers (and other kinds of sequence, such as iostreams), where other languages generally limit you to a few specific algorithms built into their containers.
Ease of use and learning ought to be a priority in the design of these objects, to the extent it is efficient and safe. To use iterators in lieu of keys or indices seems to me like a great simplification for users. It completely obviates the need to understand iterators in this case (while still allowing existing iterator functionality).
I suspect it would also lead to great confusion over what iterators are, which I think is fundamentally important to making good use of the standard library. Ben.