Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do the std::equal overloads do?

Tags:

stl

c++14

I'm looking at the recent c++14 overloads for std::equal, and I can't figure out just what they do and are used for...

The two overloads are:

template< class InputIt1, class InputIt2 >
bool equal( InputIt1 first1, InputIt1 last1, 
            InputIt2 first2, InputIt2 last2 );

template< class InputIt1, class InputIt2, class BinaryPredicate >
bool equal( InputIt1 first1, InputIt1 last1, 
            InputIt2 first2, InputIt2 last2,
            BinaryPredicate p );

I fully understand the traditional std::equal that uses just one InputIt2, but the second InputIt2 last2 is twisting my brain. Could someone explain and give an example of this?

like image 918
Syntactic Fructose Avatar asked Jun 16 '14 20:06

Syntactic Fructose


People also ask

Can we overload == operator?

Overloading Binary Operators Suppose that we wish to overload the binary operator == to compare two Point objects. We could do it as a member function or non-member function. To overload as a member function, the declaration is as follows: class Point { public: bool operator==(const Point & rhs) const; // p1.

What is std :: equal?

The C++ function std::algorithm::equal() tests whether two sets of element are equal or not. Size of the both set need not to be equal. It uses binary predicate for comparison.

What is the purpose of function overloading in C++?

C++ lets you specify more than one function of the same name in the same scope. These functions are called overloaded functions, or overloads. Overloaded functions enable you to supply different semantics for a function, depending on the types and number of its arguments.

What are overloaded operators in C++?

An overloaded operator is called an operator function. You declare an operator function with the keyword operator preceding the operator. Overloaded operators are distinct from overloaded functions, but like overloaded functions, they are distinguished by the number and types of operands used with the operator.


1 Answers

The new overloads are actually pretty great. You pass in two full ranges, beginning and end, and rather than run off the end of the shorter one and invoke undefined behaviour, the algorithm stops.

Such improvements were also added to std::mismatch and std::is_permutation. You can read more about this in the proposal

For std::equal, the algorithm will simply return false if the lengths are not equal.
For std::mismatch, if the algorithm hits the end of one range, it will return that iterator and the corresponding iterator from the other range.
For std::is_permutation, the algorithm will also simply return false if the ranges are not equal in length.

For reasoning about why, consider that the programmer checking the length is not necessarily possible or cheap. A range obtained from a std::list without the original list would need to be traversed through to get the size. A range that uses an InputIterator, such as for reading from standard input, is potentially infinite until it hits an end, and it is only allowed to be traversed through once, so the algorithm could no longer use it after you do that. Thank Benjamin Lindley below for that last example.

like image 101
ghostofstandardspast Avatar answered Oct 08 '22 22:10

ghostofstandardspast