🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #1923

Re: STL find errorprone

#1923
From: Jason D. Morris
Date: Thu, 07 Sep 1995 15:04
47 lines
2148 bytes
tony@online.tmx.com.au (Tony Cook) wrote:

>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.

One reason is that you may not want to search the entire container but rather a
small portion between two iterators.  The find() algorithm allows for such
flexibility.

                                                         -Jason-

[ 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: <42f8g0$spv@ixnews6.ix.netcom.com>
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage
References: <DED5p2.I2I@online.tmx.com.au>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage
From: jdmorris@ix.netcom.com (Jason D. Morris)
Newsgroups: comp.std.c++
Subject: Re: STL find errorprone
Date: 7 Sep 1995 15:04:48 GMT
Organization: Netcom
Approved: clamage@eng.sun.com (comp.std.c++)
Message-ID: <42f8g0$spv@ixnews6.ix.netcom.com>
References: <DED5p2.I2I@online.tmx.com.au>
NNTP-Posting-Host: taumet.eng.sun.com
Content-Type: text
X-Nntp-Posting-Host: ix-pon-mi1-10.ix.netcom.com
X-Newsreader: Forte Free Agent 1.0.82
Content-Length: 1646
X-Lines: 50
Originator: clamage@taumet