🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Making STL user-friendly?? [Re: STL find errorprone]

#2003
From: Tony Cook
Date: Thu, 21 Sep 1995 05:44
69 lines
2938 bytes
Mumit Khan (khan@xraylith.wisc.edu) wrote:
: This is a rather interesting thread started by Tony Cook in c.std.c++
: on whether STL is error-prone or not.

Thanks <grin>.

: 1. What are the various things that you have find to be error-prone?

Take the find example again:
  vector<int> myContainer;
  vector<int>::iterator found = 
	find(myContainer.begin(), myContainer.end());
  if (found == myContainer.end())
	....

Here I must supply the container name 3 times in a to perform a
fairly simple (and common) operation - this is what I find
error-prone.

: 2. What kind of changes would you like to see in the interface to the
:    STL algorithms? The thread started with Tony Cook using the begin/
:    end iterator in find algorithm (which I consider essential since I 
:    do partial searches all the time) as an example if I remember
:    correctly. Cook suggests find() return a bool and modify the iterator 
:    argument ... 

I don't see any need to _remove_ the current begin/end idiom - as
you say it's useful - but to simply supply extra interfaces to
perform the most common functions in a more compact way -
specifically, operating on the whole container, and possibly
operating from the start of the container to a specific point and
from a specific point to the end of the container.

Note that even with a subset of a container, we still have multiple
redundant references, eg:

  vector<int>::iterator searchStart, searchEnd;
  // ... searchStart, searchEnd set somehow
  vector<int>::iterator searchResult = 
	find(searchStart, searchEnd);
  // we use searchEnd twice
  if (searchResult == searchEnd)
	...

or, possibly more realisticly:
  vector<int>::iterator searchStart, searchEnd;
  // ... searchStart, searchEnd set somehow
  vector<int>::iterator searchPos = searchStart;
  while (searchPos = find(searchPos, searchEnd)) != searchEnd)
  {
    //...process it
    ++searchPos; // prepare for next one
  }

The test here should be 'did we find it' - not 'are we at this point
yet' - find 'knows' where the end of the range is - why can't it
report whether or not it was successful without us having to supply
one of it's parameters again?

-- 
        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: <MATT.95Sep20214413@Austern>
Path: arc1-ca.pugleaf.net!howland.reston.ans.net!agate!agate!Matt

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!howland.reston.ans.net!agate!agate!Matt
From: Tony Cook <tony@online.tmx.com.au>
Newsgroups: comp.std.c++
Subject: Re: Making STL user-friendly?? [Re: STL find errorprone]
Date: 21 Sep 1995 05:44:13 GMT
Organization: Home
Approved: Matt@Austern
Message-ID: <MATT.95Sep20214413@Austern>
NNTP-Posting-Host: austern.hip.berkeley.edu
X-Newsreader: TIN [version 1.2 PL2]