Article View: comp.std.c++
Article #3593Re: STL pop_back()
From: Dietmar Kuehl
Date: Tue, 05 Mar 1996 17:50
Date: Tue, 05 Mar 1996 17:50
42 lines
2728 bytes
2728 bytes
Hi,
Hayden Schultz (haydens@tiac.net) wrote:
: Why don't the pop functions return a copy of the object before they
: blow it away?
These containers are simply not intended to be used as stack (or queue)
directly. Instead, you can use 'stack<Container<T> >' or
'queue<Container<T> >' to use the class 'Container' as stack (or
queue). These adaptors can be implemented in terms of 'push_back()' and
'pop_back()'. However, 'stack<...>::pop()' also returns 'void'. The
reason is simple: If you need the value, you can get it with a sequence
of 'top()' and 'pop()' operations. Requiring that 'pop()' returns an
object would make it necessary to create this object which in turn can
be expensive. This way it is defined everything is possible
efficiently.
: Another possibility, I suppose, would be an auto_ptr<T>
: so that the object is deleted if it's not used. An auto_ptr<T> is
: about as efficient as deleting it in the pop function, it just changes
: the time it's deleted (which isn't guaranteed anywhere I've read
: anyway).
Note, that STL containers in general store objects, not pointers.
Returning an 'auto_ptr<T>' would require that pointers are stored. This
is inefficient e.g. if you want to store 'int's.
: Why call the methods push and pop if it doesn't act like a stack?
These functions are e.g. used to implement a stack adapter which acts
like a stack or a queue.
--
dietmar.kuehl@uni-konstanz.de
http://www.informatik.uni-konstanz.de/~kuehl
I am a realistic optimist - that's why I appear to be slightly pessimistic
---
[ 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
]
Technical Details
Message-ID:
<4hhsta$2gk@news.BelWue.DE>Path:
arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austernReferences:
<4h4hmr$41o@news.rwth-aachen.de> <4h78mr$alj@engnews1.Eng.Sun.COM> <313BD2CA.7DBD@tiac.net>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austern From: kuehl@uzwil.informatik.uni-konstanz.de (Dietmar Kuehl) Newsgroups: comp.std.c++ Subject: Re: STL pop_back() Date: 05 Mar 1996 09:50:19 PST Organization: Fakultdt f|r Mathematik und Informatik Approved: austern@isolde.mti.sgi.com Message-ID: <4hhsta$2gk@news.BelWue.DE> References: <4h4hmr$41o@news.rwth-aachen.de> <4h78mr$alj@engnews1.Eng.Sun.COM> <313BD2CA.7DBD@tiac.net> Reply-To: dietmar.kuehl@uni-konstanz.de NNTP-Posting-Host: isolde.mti.sgi.com X-Original-Date: 5 Mar 1996 17:16:26 GMT X-Newsreader: TIN [version 1.2 PL2] X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBVAwUBMTx/Gky4NqrwXLNJAQHq0wIAillW5NqNOJSOCNnkPo+N9YdwezUFnlZj lid7XFh8fxL8hAi2Vf0snT0DFEowA4Kt59acbfyhPmZkh3L0S5CeUQ== =N83Y Originator: austern@isolde.mti.sgi.com