Tech Off Thread

3 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

What's the reasoning behind the behavior of c++ "std::equal"?

Back to Forum: Tech Off
  • User profile image

    Hi all,

    I was just watching the C9 lecture from Stephan about STL algorithms and functors, and I can't really wrap my head around std::equal!

    I mean, for me the semantics is a bit strange, since what it does is IMHO more like "isPrefixOf(...)" than an equality check. What was the idea behind implementing std::equal as it is? Is the functionname "equal" not a bit misleading for what it really does?





  • User profile image
    Sven Groot

    @McMaiersen: Well, it does test two ranges for equality, it's just that the length of the range to be checked is determined by the first iterator pair. Perhaps it is a peculiar design, but it is in line with how most STL functions that operate on two ranges only require you to specify the end of one of them.

    Also note that this function crashes (or at least has undefined behavior) if the second range is shorter than the first one, so you have to do sanity checking of the lengths before hand anyway.

  • User profile image

    The "equal" function is in every language for doing a "deep"-check on two objects.

    It ensures, that, as the name implies, both objects are actually equal. Consider the following the string construction in class c++:

    char *String1 = "Hello World";char *String2= "Hello World";

    In more simple languages like VB, you could just compare the strings by "String1 = String2", in more complex languages this wouldn't work because the content will beplaced at two different memory locations, thus an error will be returned.

    The "equal" function now does check the real content of the string pointers.



    Founder / | Lelala

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.