equal in algorithm.h and userdefined equal

Hello guys,

i wanted to ask what differences there are between this userdefined equal bool

1
2
3
4
5
6
7
8
9
  bool equal(const string& a, const string& b)
{
    if (a.size() != b.size())
        return false;
    for (size_t i = 0; i < a.size(); ++i)
        if (toupper(a[i]) != toupper(b[i]))
            return false;
    return true;
}


and the equal command with the <algorithm> include.

2. how to use the equal command in algorithm? in this userdefined i use it like this:

 
if(equal(answer, "yes"))
std::equal() takes the arguments in terms of iterators.
https://en.cppreference.com/w/cpp/algorithm/equal

So if(equal(answer, "yes")) wouldn't have compiled. std::equal can be used along anything that can give input & output iterators.
Last edited on
Your user-defined function will test for equality regardless of case (upper or lower).
The standard algorithm will see "YES" and "yes" as different. In this respect, your user-defined function will probably do what you want.

If you want to use std::equal as in <algorithm> you will need to use the form with a predicate, which will return the test for equality regardless of case.


I wouldn't deliberately name a user-defined function the same as one in the standard library, especially if they did different things.

Last edited on
Topic archived. No new replies allowed.