Topic: More on the operations on the entire container


Author: "Eugene Radchenko" <eugene@qsar.chem.msu.su>
Date: 1996/01/17
Raw View
Hi!
Operations on the whole container are (as the threads flashing here
sometimes show) the most common kind of range operations in a program using
the STL. Hence, it is generally felt that the current idiom for this
  Iterator i = find(c.begin(), c.end(), x);
is verbose, cumbersome and error-prone. However, no solution (fitting into
the split container/algorithm paradigm) has been proposed yet.
I think this can do the trick:
  template <class Iterator> struct range : public pair<Iterator, Iterator> {
   range(Iterator first, Iterator last)
      : pair<Iterator, Iterator>(first, last) {}
  };
  template <...> class vector  {
    //....
    range<iterator> all()  { return range(begin(), end()); }
    range<const_iterator> all() const { return range(begin(), end()); }
  };
Also, the library would provide the 'range' version of all functions, e.g.
  template<class Iterator, class T> Iterator find(range<Iterator> r, T x);
simply inlining to find(r.first, r.second, x).

(If the conversions were allowed in the template deduction, it would be
even simpler with
   operator range<iterator>()
in vector).

     So, what do you think?                            Genie

--
--------------------------------------------------------------------
Eugene V. Radchenko           Research associate, Computer Chemistry
E-mail: eugene@qsar.chem.msu.su                Fax: +7-(095)939-0290
Ordinary mail:  Chair of Organic Chemistry, Department of Chemistry,
                      Moscow State University, 119899 Moscow, Russia
*****************  Disappearances are deceptive  *******************
---
[ 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. ]





Author: David Byrden <100101.2547@compuserve.com>
Date: 1996/01/18
Raw View
Genie;

>> Operations on the whole container are (as the threads flashing here
>> sometimes show) the most common kind of range operations in a program
>> using the STL. Hence, it is generally felt that the current idiom for
>> this    Iterator i = find(c.begin(), c.end(), x);  is verbose,
>>  cumbersome and error-prone.


>>   So, what do you think?

I think it's 6 months too late to propose changes to the standard!  :)

I keep a little library of template functions;


template< class Container, class Functor >
 void  for_contents(  Container& c, Functor  f  )
 {
    for_each(  c.begin(), c.end(), f  ) ;
 }


Problem solved?


             David
---
[ 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. ]