Topic: Why is there no std::string::back()?
Author: clarkcox3@yahoo.com (Clark S. Cox, III)
Date: Wed, 24 Jan 2001 23:11:55 GMT Raw View
Joel de Guzman <joel@interxys.com> wrote:
> "Clark S. Cox III" <clarkcox3@yahoo.com> wrote in message
> news:1en4xsl.146mtez1patx9iN%clarkcox3@yahoo.com...
> > Scott Robert Ladd <scott@coyotegulch.com> wrote:
> >
> > > Hi,
> > >
> > > I've often wondered why Standard classes -- and "string" in
> particular --
> > > lack convenience methods commonly found in other languages. No "back()",
> nor
> > > any "left()" or "right". I suppose it can (and probably will) be argued
> that
> > > such methods can be constructed from "substr()" -- but then, why include
> > > "push_back(x)" for containers, when it is simply an alias for
> > > "insert(v.end(),x)"?
> >
> > In some cases, push_back() is more efficent than insert(v.end(),x)
> > (i.e. for back insertion sequences). So I think that push_back() isn't
> > provided for convienence, but for efficiency.
> >
>
> Could you explain why we can't introduce that
> efficiency in the insert(...)?
That would require an extra check to see if the first parameter to
insert() was equal to v.end(), and I would think that since it is much
more common to call insert() to insert in the middle of the sequence,
that check would usually be wasted time. If someone really wanted to add
to the end of the sequence, they should use push_back().
--
http://www.whereismyhead.com/clark/
Clark S. Cox, III
clarkcox3@yahoo.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: "Joel de Guzman" <joel@interxys.com>
Date: Wed, 24 Jan 2001 14:57:31 GMT Raw View
"Clark S. Cox III" <clarkcox3@yahoo.com> wrote in message
news:1en4xsl.146mtez1patx9iN%clarkcox3@yahoo.com...
> Scott Robert Ladd <scott@coyotegulch.com> wrote:
>
> > Hi,
> >
> > I've often wondered why Standard classes -- and "string" in
particular --
> > lack convenience methods commonly found in other languages. No "back()",
nor
> > any "left()" or "right". I suppose it can (and probably will) be argued
that
> > such methods can be constructed from "substr()" -- but then, why include
> > "push_back(x)" for containers, when it is simply an alias for
> > "insert(v.end(),x)"?
>
> In some cases, push_back() is more efficent than insert(v.end(),x)
> (i.e. for back insertion sequences). So I think that push_back() isn't
> provided for convienence, but for efficiency.
>
Could you explain why we can't introduce that
efficiency in the insert(...)?
Joel de Guzman
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: "Scott Robert Ladd" <scott@coyotegulch.com>
Date: Fri, 12 Jan 2001 20:54:32 GMT Raw View
Hi,
I've often wondered why Standard classes -- and "string" in particular --
lack convenience methods commonly found in other languages. No "back()", nor
any "left()" or "right". I suppose it can (and probably will) be argued that
such methods can be constructed from "substr()" -- but then, why include
"push_back(x)" for containers, when it is simply an alias for
"insert(v.end(),x)"?
I certainly wouldn't advocate a kitchen-sink approach to designing the
STL -- but as the STL stands now, it seems to be an odd mixture of
complexity and simplicity. Why include several container classes, but none
that are ordered or hash-based? Why include "vector::push_back" but not
"string::left?" It all seemes vaguely incomplete, yet overly-complicated.
I don't mean this as a criticism of the C++ committee; I'm just mystified as
to "why" the STL evolved as it did.
--
Scott Robert Ladd
http://www.coyotegulch.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: clarkcox3@yahoo.com (Clark S. Cox III)
Date: Mon, 15 Jan 2001 20:19:47 CST Raw View
Scott Robert Ladd <scott@coyotegulch.com> wrote:
> Hi,
>
> I've often wondered why Standard classes -- and "string" in particular --
> lack convenience methods commonly found in other languages. No "back()", nor
> any "left()" or "right". I suppose it can (and probably will) be argued that
> such methods can be constructed from "substr()" -- but then, why include
> "push_back(x)" for containers, when it is simply an alias for
> "insert(v.end(),x)"?
In some cases, push_back() is more efficent than insert(v.end(),x)
(i.e. for back insertion sequences). So I think that push_back() isn't
provided for convienence, but for efficiency.
--
http://www.whereismyhead.com/clark/
Clark S. Cox, III
clarkcox3@yahoo.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: "Jim Cobban" <thesnaguy@hotmail.com>
Date: Fri, 12 Jan 2001 04:53:35 GMT Raw View
I am puzzled as to why std::string implements almost all of the same
interface as std::vector but does not implement back(). I find it very
common to examine the last character in a string, but the way I am expected
to do this, according to several programming guides, is mystr[mystr.size() -
1], which looks really clumsy and error-prone. std::string does implement
rbegin() so the last character may also be examined by *mystr.rbegin() but
that is not as readable as mystr.back() would be.
Of course std::string also does not implement front() but the equivalent
mystr[0] is just as readable.
--
Jim Cobban jcobban@magma.ca
34 Palomino Dr.
Kanata, ON, CANADA
K2M 1M1
+1-613-592-9438
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]