boost::algorithm::equal() in C++ library

The equal() function in C++ boost library is found under the header ‘boost/algorithm/cxx11/equal.hpp’ tests to see if two sequences contain equal values. It returns a boolean value which determines if both the sequences are same or not.

Syntax:

bool equal ( InputIterator1 first1, InputIterator1 second1,
                  InputIterator2 first2, InputIterator2 second2 )
or 
bool equal ( InputIterator1 first1, InputIterator1 second1,
             InputIterator2 first2, InputIterator2 second2, 
             BinaryPredicate pred )

Parameters: The function accepts parameters as described below:



  • first1: It specifies the input iterators to the initial positions in the first sequence.
  • second1: It specifies the input iterators to the final positions in the first sequence.
    • first2: It specifies the input iterators to the initial positions in the second sequence.
    • second2: It specifies the input iterators to the final positions in the second sequence.
    • p: It specifies the comparison predicate if specified.

    Return Value: The function returns true if both the sequence are same, else it returns false.

    Note: The above function runs for C++14 and above.

    Program-1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to implement the
    // above mentioned function
      
    #include <bits/stdc++.h>
    #include <boost/algorithm/cxx14/equal.hpp>
    using namespace std;
      
    // Drivers code
    int main()
    {
      
        // Declares the sequences
        int a[] = { 1, 2, 5, 6, 8 };
      
        int b[] = { 1, 2, 5, 6, 8 };
      
        // Run the function
        bool ans
            = boost::algorithm::equal(a, a + 5, b, b + 5);
      
        // Condition to check
        if (ans == 1)
            cout << "Sequence1 and Sequence2 are equal";
        else
            cout << "Sequence1 and Sequence2 are not equal";
        return 0;
    }

    chevron_right

    
    

    Output:

    Sequence1 and Sequence2 are equal
    

    Program-2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to implement the
    // above mentioned function
      
    #include <bits/stdc++.h>
    #include <boost/algorithm/cxx14/equal.hpp>
    using namespace std;
      
    // Drivers code
    int main()
    {
      
        // Declares the sequences
        int a[] = { 1, 2, 5, 6, 8 };
      
        int b[] = { 1, 2, 5 };
      
        // Run the function
        bool ans
            = boost::algorithm::equal(a, a + 5, b, b + 5);
      
        // Condition to check
        if (ans == 1)
            cout << "Sequence1 and Sequence2 are equal";
        else
            cout << "Sequence1 and Sequence2 are not equal";
        return 0;
    }

    chevron_right

    
    

    Output:

    Sequence1 and Sequence2 are not equal
    

    Reference: https://www.boost.org/doc/libs/1_70_0/libs/algorithm/doc/html/algorithm/CXX14.html#the_boost_algorithm_library.CXX14.equal



    My Personal Notes arrow_drop_up

    The function of education is to teach one to think intensively and to think critically

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




    Article Tags :
    Practice Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.