Suppose I have a template function which takes an OutputIterator of pair<X,Y>s which should now be used to fill a std::map<X,Y>:
1 2 3 4 5 6 7 8 9 10 11 12
<templatetypename OutputIterator>void f(OutputIterator result)
{
for(int i=0; i<10; ++i)
result++ = std::pair<X,Y>(X(...), Y(...));
}
void g()
{
std::map<X,Y> m;
f(std::back_inserter(m));
// ^^ does not exist! (map::push_back does not exist)
}
Obviously, there is no {back,front}_inserter for std::map, since it does not have the operations push_{front,back}. Is there any way to use the function interface mentioned above nonetheless?
Wait. Isn't std::map just a balanced binary search tree? There's no point in inserting something in the middle, since it's an sorted structure to begin with. If you use
map[key]=value; //'key' didn't previously exist in the map.
Yes, but you can't use [] with STL (or other generic) algorithms.
For associative containers insert() sticks the item in the correct spot in the tree, so any iterator you supply to start will work just fine.