Article View: comp.std.c++
Article #1901STL find errorprone
From: Tony Cook
Date: Mon, 04 Sep 1995 15:36
Date: Mon, 04 Sep 1995 15:36
41 lines
1761 bytes
1761 bytes
I've been using the STL a little recently and can't really call
myself an expert - but I've found that always typing in the
container's name up to three times for a find or twice for other
actions is getting a little error-prone, for example:
// check if already there
vector<int> myVector; // this is a _short_ name
if (find(myVector.begin(), myVector.end(), someInt)
== myVector.end())
{
// not there
}
I find this more than a little error-prone, why isn't there
something like:
template <class Container, class Item>
bool find(Container &c, Item const &v, Container::iterator &i)
{
i = find(c.begin(), c.end(), v);
return i != c.end();
}
So the above becomes:
Container::iterator i;
if (!find(myVector, someInt, i))
{
// not there
}
One argument against this is 'the programmer can write their own' -
but this is true for most things - the libraries are there to
provide a useful (and hopefully less error-prone) implementation so
the programmer doesn't have to write their own.
--
Tony Cook - tony@online.tmx.com.au
100237.3425@compuserve.com
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<DED5p2.I2I@online.tmx.com.au>Path:
arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamageShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage From: tony@online.tmx.com.au (Tony Cook) Newsgroups: comp.std.c++ Subject: STL find errorprone Date: 4 Sep 1995 15:36:40 GMT Organization: Home Approved: clamage@eng.sun.com (comp.std.c++) Message-ID: <DED5p2.I2I@online.tmx.com.au> NNTP-Posting-Host: taumet.eng.sun.com X-Newsreader: TIN [version 1.2 PL2] Originator: clamage@taumet