Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ STL algorithm equal

One particularly useful standard algorithm is std::equal, which is defined as follows:

template <typename InputIterator1, typename InputIterator2>
inline bool equal(InputIterator1 start1,
InputIterator1 end1,
InputIterator2 start2)
{
    while(start1 != end1)
    {
        if(*start1 != *start2) return false;
        ++start1;
        ++start2;
    }
    return true;
}

The algorithm walks over the range defined by [start1, end1) and [start2, start2 + (end1 – start1)) and returns whether the elements in the range are equal. Notice that the algorithm is templatized over two different types of input iterators.

Why is this?

like image 944
Tika Gurung Avatar asked Jun 22 '17 15:06

Tika Gurung


People also ask

What does equal () do in C++?

C++ Algorithm equal()function compares the elements in both the containers and returns a true value if all the elements in both the containers are found to be matching.

What is STD equal?

std::equal Compares the elements in the range [first1,last1) with those in the range beginning at first2 , and returns true if all of the elements in both ranges match. The elements are compared using operator== (or pred , in version (2)). The behavior of this function template is equivalent to: 1.


3 Answers

So far you've gotten two answers that focus on containers. That's the wrong focus. The fundamental data abstraction in the STL is the sequence. A sequence is defined by a pair of iterators. Containers are one way of managing sequences, but they are not the only way. So, to give the right <g> answer:

std::equal compares two sequences for equality. There is no good reason to limit the application of the algorithm to sequences that have the same iterator type, so there is no such limit. The sequences may have different origins, and may refer to different value types.

For example, you might want to check whether the values represented in a file that holds a text represention of double values is identical to the content of a vector of integers stored in memory. The vector defines a sequence; you can get at its iterators with begin() and end(). The file defines a sequence; you can get at its iterators by opening the file with an ifstream and creating a pair of istream_iterator<double> objects. std::equal (and all the rest of the standard algorithms) will work just fine with these disparate sources of data and their different data types.

like image 60
Pete Becker Avatar answered Oct 29 '22 11:10

Pete Becker


Lets say you have a std::list<int> and std::vector<int> and want to see if they are equal. If std::equal did not take different iterator types you could not use it since std::list<int>::iterator is not the same type as std::vector<int>::iterator.

This also applies to the same container type but storing different elements. A std::vector<int>::iterator is not the same as a std::vector<long long>::iterator and so you would not be able to compare those either if it used the same type for both iterator pairs.

like image 42
NathanOliver Avatar answered Oct 29 '22 09:10

NathanOliver


It is templatized this way so you can pass either two iterators of the same type or two iterators with different types.



For instance:

vector<int> a; //some stuff
list<int> b; //some stuff
equal(a.begin(), a.end(), b.begin());
/*
Here InputIterator1 is a vector<int>::iterator
and InputIterator2 is a list<int>::iterator
*/

vector<double> c; //some stuff
vector<double> d; //some stuff
equal(c.begin(), c.end(), d.begin());
/*
Here InputIterator1 is a vector<double>::iterator
and InputIterator2 is also a vector<double>::iterator
*/
like image 31
Vivick Avatar answered Oct 29 '22 11:10

Vivick