Topic: Count signature


Author: phalpern@truffle.ma.ultranet.com (Pablo Halpern)
Date: 1998/05/19
Raw View
dietmar.kuehl@claas-solutions.de wrote:

>
>  template <typename It, typename T>
>  typename iterator_traits<It>::size_type
>  count(It begin, It end, T const &val);
>
>The reason for the original version was simply that at this time there was
>apparently no way to determine the size_type associated with the iterator.
>Thus, the user was required to pass an object of appropriate type. With
>'iterator_traits' the interface could be made safer (ie. the user cannot mess
>up the type of the count anymore) and more convenient.

The adapted SGI (and I think ObjectSpace) libraries have both versions,
depending on the capabilities of your compiler. I think this is
wrong-headed.  For compilers that can't support traits classes, I think
the better definition is:

  template <typename It, typename T>
  unsigned long count(It begin, It end, T const &val);

This allows users of slightly outdated compilers to use the newer
calling convention without worrying about changing it when they upgrade
compilers. The only time unsigned long is inappropriate is if you define
your own size type which is larger than unsigned long. If you are doing
that kind of thing, then I doubt the older compilers will be up to the
task anyway. So lets just keep it simple for the 99% case.  Anybody from
the SGI adaptation effort listening?

When I teach STL on older compilers, I provide a header file that
defines count() as above and defines distance() similarly. It makes it
easy to compile my lab solutions with both old and new compilers.

BTW, I also get around the std:: namespace problem by using adding the
-Dstd= option to compiler commands for most compilers.  This causes
std::cout to be preprocessed into just ::cout, which works great for
compilers without namespaces.

-------------------------------------------------------------
Pablo Halpern                   phalpern@truffle.ultranet.com

I am self-employed. Therefore, my opinions *do* represent
those of my employer.


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: dietmar.kuehl@claas-solutions.de
Date: 1998/05/11
Raw View
Hi,
In article <6j0bmk$m0v@bgtnsc03.worldnet.att.net>,
  Cristian.Georgescu@worldnet.att.net wrote:
> The "count" algorithm as specified by the standard returns the
> result in a parameter passed by reference;
>
> In C++PL 3 page 526 (18.5.3)
> the same count algorithm seems to return the result as the return value
> of the function.
>
> Which information is the newest?

The definition in FDIS (final draft international standard) returns the value.
The orginal definition of 'count()' was not really satisfactory: having to
pass an object to be used as count is inconvenient at best. With the
introduction of 'iterator_traits' a better approach became available: the
'size_type' can be used to store the count and is even guaranteed to have an
appropriate size:

  template <typename It, typename T>
  typename iterator_traits<It>::size_type
  count(It begin, It end, T const &val);

The reason for the original version was simply that at this time there was
apparently no way to determine the size_type associated with the iterator.
Thus, the user was required to pass an object of appropriate type. With
'iterator_traits' the interface could be made safer (ie. the user cannot mess
up the type of the count anymore) and more convenient.

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/   Now offering spam-free web-based newsreading
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Cristian Georgescu <Cristian.Georgescu@worldnet.att.net>
Date: 1998/05/09
Raw View
Count signature
===============

The "count" algorithm as specified by the standard returns the
result in a parameter passed by reference;

In C++PL 3 page 526 (18.5.3)
the same count algorithm seems to return the result as the return value
of the function.

Which information is the newest?

Here is the extract from the standard:

C++ language Standard Draft
===========================

template<class InputIterator, class T, class Size>

void count(InputIterator first, InputIterator last, const T& value,

Size& n);

template<class InputIterator, class Predicate, class Size>

void count_if(InputIterator first, InputIterator last, Predicate pred,
Size& n);

Effects: Adds to n the number of iterators i in the range [ first, last)
for which the following corresponding conditions hold: *i == value, pred
(*i) == true.
Complexity: Exactly last - first applications of the corresponding
predicate.
Notes: count must store the result into a reference argument instead of
returning the result because the size type cannot be deduced from
built-in iterator types such as int*.

--
Cristian Georgescu
_________________________________________________
    Smiths Industries
    Aerospace & Defense Systems
    7-9 Vreeland Road,
    Florham Park, NJ 07932, USA.


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]