Topic: STL count, find: std syntax?


Author: Joerg Barfurth <jbarfurth@vossnet.de>
Date: 1999/06/12
Raw View
Peter Claessens wrote:
>
> Dear all,
>
> I'm experiencing some troubles to write an ANSI-compliant application -
> I see different declarations of the find and count functions. Take a
> look at this sourcecode sample:
>
> ...
> map<string, string> varval;
> ...
> string varstr;
> ...
> int n;
> //    n = count(varval.begin(),varval.end(),varstr);
> //    n = 0; count (varval.begin(),varval.end(),varstr,n);
>     n = varval.count (varstr);
>
> My compiler only accepts the last (uncommented) version. But only the 2
> previous are standard, and of those 2, only the second is not
> deprecated.
> Is this information correct?

Your compiler seems to be correct. By Table 69 and 23.3.1 in the standard a
specialization M of ::std::map has a member function M::size_type
M::count(const M::key_type& k) which returns the number of occurrences of k as
a _key_ in the container [which will always be 0 or 1 for a (non-multi) map].
I couldn't find the second (commented) version you describe anywhere in the
standard.
The first one shouldn't compile (as seems to be the case): To use
std::count<InIt,T>() the object obtained by dereferencing an InIt must be of
type T (or convertible to it) - iirc.
But in your case, the value_type of map<string,string> is pair<string
const,string> which is not compatible with a single string.

So to count the number of occurrences of a value as the mapped value of a map
entry, you need to use something like:
 template<class Map>
 struct MappedValueIs
 : std::unary_function<typename Map::value_type, bool>
 {
  typename Map::mapped_type valueToFind;
  MappedValueIs(typename Map::mapped_type const& value) : valueToFind(value) {}

  bool operator()(typename Map::value_type const& aPair) const {
   return aPair.second == valueToFind;
  }
 };

 template<typename Map>
 int count_mapped(Map const& map, typename Map::mapped_type const& value) {
  return std::count_if(map.begin(),map.end(), MappedValueIs<Map>(value) );
 }

 // now you can do - referring to your declarations:
 n = count_mapped(varval,varstr);

The case of find is analogous.

> Thanks,
>
> Peter.

HTH J   rg Barfurth


[ 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: Peter Claessens <peter.claessens@psy.kuleuven.ac.be>
Date: 1999/06/03
Raw View
Dear all,

I'm experiencing some troubles to write an ANSI-compliant application -
I see different declarations of the find and count functions. Take a
look at this sourcecode sample:

...
map<string, string> varval;
...
string varstr;
...
int n;
//    n = count(varval.begin(),varval.end(),varstr);
//    n = 0; count (varval.begin(),varval.end(),varstr,n);
    n = varval.count (varstr);

My compiler only accepts the last (uncommented) version. But only the 2
previous are standard, and of those 2, only the second is not
deprecated.
Is this information correct?

Thanks,

Peter.


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