Re: count_unique or unique_count - why does it not exist?
In article <PUn_g.16755$e66.11901@newssvr13.news.prodigy.com>, Gene
Bushuyev <spam@spamguard.com> wrote:
. Below is the code off the top of
my head,
so it may contain errors, but the idea I think should be clear:
std::size_t sum(std::size_t count, bool equal)
{
return equal ? count : count + 1;
}
template<class It>
std::size_t unique_count(It first, It last)
{
return first != last ?
std::inner_product(first++, last, first, 1, sum,
std::equal_to<typename
It::value_type>())
: 0;
}
This is what I think you want, assumes bool is convertable to size_t.
and For is a forward iterator.
template <class For>
std::size_t unique_count(For first,For last)
{
if(first==last) return 0;
// need a copy since order of argument evaluation
// is unspecified in the standard.
For x(first);
return std::inner_product
(
first,
last,
++x,
(unsigned int)0,
std::plus<std::size_t>(),
std::not_equal_to
<
std::iterator_traits<For>::value_type
>()
);
}
I do not see the gain over using an output iterator to compute the
result, via unique_copy(). [see another post in this thread for
details]
in my opinion the output iterator solution is better, can be done with
boost::function_output_iterator like
class unique_count
{
struct internal
{
std::size_t &n;
internal(std::size_t &a):n(a){}
template <class T>
void operator ()(T const &){++n;}
};
std::size_t n;
public:
unique_count():n(0){}
template <class In>
std::size_t operator ()(In first,In last)
{
std::unique_copy
(
first,
last,
boost::function_output_iterator
<
internal
>(internal(n))
);
return n;
}
};
usage:
unique_count()(first,last)
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]