Topic: set::insert with hint (Was: STL Inheritance Question)


Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1997/12/16
Raw View
In a thread in clcm, I wrote the following template function:

template <class T, class Allocator> // untested code
inline void insert_behind (list<T, Allocator>& li, const T& item,
                           const T& behindItem)
{
    list<T, Allocator>::iterator it =
            find (li.begin (), li.end (), behindItem);
    if (it == li.end ()) throw "not found";
    li.insert (++it, item);
}

Now, notice that is only make sens for sequences. Other
containners, like associative ones, won't work.

Well, while instantiating the algo on set (everything is
done in my head, without a real compiler, of course), I
noticed that it worked. It only works because there is a
function signature 'insert (iterator, const value_type&)'
in set which is that same as for sequences (ie vector,
list, etc). So this function is called; but it doesn't
insert at a position, it's semantically equivalent with
an 'insert (const value_type&)' except for complexity.

Why does this function, semantically equivalent to one
arg insert, take the same parameters as the 'insert here'
function is sequences ?

Functions with identicall semantics in different classes
have the same name, for genericity; now shouldn't functions
with very different semantics get different names ?

Why isn't it called insert_hint ? It would save confusion
(not for me, but for many users).

--

Valentin Bonnard                mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://www.pratique.fr/~bonnardv/
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]