New to OO concepts - re-usability

Alex Martelli aleaxit at yahoo.com
Thu Feb 22 18:10:21 CET 2001


"Rainer Deyke" <root at rainerdeyke.com> wrote in message
news:nHal6.285701$ge4.97547638 at news2.rdc2.tx.home.com...
> "Chris Hanson" <cmh at bDistributed.com> wrote in message
> news:220220010813598042%cmh at bDistributed.com...
> > I've actually had STL proponents tell me it's a *good* thing that
> > std::vector<T> doesn't have a push_front() method because if you try to
> > do that your code won't compile and you'll know you should be using a
> > "more efficient" container class!  What if I *know* it's an expensive
> > operation, I know I need to do it occasionally, and I need the speed
> > that std::vector<T> will otherwise give me?  "Subclass std::vector<T>
> > then!"  (This was the real answer they gave.)  If I have to subclass a
> > standard container class to get simple functionality, it's *not*
> > well-designed.
>
> The correct answer, of course, is to use the 'insert' method.

I'd rather do a push_back and a rotate because, in my typical
use, N/2 swaps would be far less costly than N-1 copies.  But,
sure, "vec.insert(vec.begin(),x);" IS rather more concise than
"vec.push_back(x); std::rotate(vec.begin(),vec.end()-1,vec.end();"
(and unless you've specialized the swap template, so that the
swaps ARE fast, performance will be equivalent; however, note
that several specialized implementations of std::swap exist,
and that's what std::rotate is supposed to be using).


Alex








More information about the Python-list mailing list