Discussion:
Which comparision operator should be used inside std::find()?
(too old to reply)
Vladimir Grigoriev
2009-12-16 12:38:23 UTC
Permalink
It is interesting: Does the C++ standard say explicitly which comparision
operator should be used inside the algorithm std::find or it is
implementation dependent?

For example I can write the find() using an inequality operator

template <typename InputIterator, typename T>

InputIterator find( InputIterator first, InputIterator last, const T
&value )
{
while ( ( first != last ) && ( *first != value ) ) ++ first;
return ( first );
}

Or I can write the same using an equality operator

template <typename InputIterator, typename T>

InputIterator find( InputIterator first, InputIterator last, const T
&value )
{
for ( ; first != last; ++ first )
{
if ( *first == value ) break;
}
return ( first );
}

Vladimir Grigoriev
Victor Bazarov
2009-12-16 13:03:38 UTC
Permalink
Post by Vladimir Grigoriev
It is interesting: Does the C++ standard say explicitly which comparision
operator should be used inside the algorithm std::find or it is
implementation dependent?
For example I can write the find() using an inequality operator
template <typename InputIterator, typename T>
InputIterator find( InputIterator first, InputIterator last, const T
&value )
{
while ( ( first != last ) && ( *first != value ) ) ++ first;
return ( first );
}
Or I can write the same using an equality operator
template <typename InputIterator, typename T>
InputIterator find( InputIterator first, InputIterator last, const T
&value )
{
for ( ; first != last; ++ first )
{
if ( *first == value ) break;
}
return ( first );
}
==

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Igor Tandetnik
2009-12-16 13:02:05 UTC
Permalink
Post by Vladimir Grigoriev
It is interesting: Does the C++ standard say explicitly which comparision
operator should be used inside the algorithm std::find or it is
implementation dependent?
25.1.2 Find [lib.alg.find]
template<class InputIterator, class T>
InputIterator find(InputIterator first, InputIterator last,
const T& value);
1 Requires: Type T is EqualityComparable (20.1.1).

Table 28 in 20.1.1 defines EqualityComparable as the type such that, for two values a and b of this type, expression a==b is valid and induces an equivalence relation.
Post by Vladimir Grigoriev
For example I can write the find() using an inequality operator
template <typename InputIterator, typename T>
InputIterator find( InputIterator first, InputIterator last, const T
&value )
{
while ( ( first != last ) && ( *first != value ) ) ++ first;
return ( first );
}
That won't be a conforming implementation. It places requirements on T beyond those of EqualityComparable.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Loading...