Tech Off Thread

3 posts

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

Back to Forum: Tech Off
  • User profile image
    McMaiersen

    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?

     

     

    Regards,

    Chris

  • 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
    Lelala

    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.

    Regards,

    Flo

    Founder / http://www.lelala.de | Lelala

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.