Use of equal_to function object

I am trying to use the function object equal_to to find an element in ivec with the same value. Is it possible to use equal_to as a predicate for find_if? If so, how it is done?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//	C21.cpp
#include "std_lib_facilities.h"
#include <functional>


int main()
{
	vector<int> ivec {0, 2, 6, 4, 8, 9, 3, 7, 5, 1 };
	auto p = find_if(ivec.begin(), ivec.end(), equal_to < int > {});
	if (p != ivec.end()) {
		cout << "found " << *p << endl;
	}
	else {
		cout << "not found" << endl;
	}
}
Last edited on
Line 9: It's a function. Change the {} to ().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream> // "std_lib_facilities.h"
#include <vector>
#include <algorithm>

#include <functional>
#include <cassert>

int main()
{
    const std::vector<int> ivec { 0, 2, 6, 4, 8, 9, 3, 7, 5, 1 };

    for( int value : { 9, -7 } )
    {
        std::cout << "find " << value << " in range: " ;
        using std::placeholders::_1 ;

        const auto p1 = std::find_if( ivec.begin(), ivec.end(), std::bind( std::equal_to<int>{}, _1, value ) );
        const auto p2 = std::find_if( ivec.begin(), ivec.end(), std::bind( std::equal_to<int>(), _1, value ) );

        const auto p3 = std::find_if( ivec.begin(), ivec.end(), std::bind( std::equal_to<>{}, _1, value ) ); // C++14
        const auto p4 = std::find_if( ivec.begin(), ivec.end(), std::bind( std::equal_to<>(), _1, value ) ) ; // C++14

        const auto p5 = std::find_if( ivec.begin(), ivec.end(), [value]( int v ) { return v == value ; } ); // simple
        const auto p6 = std::find( ivec.begin(), ivec.end(), value ); // simplest

        assert( (p1==p2) && (p2==p3) && (p3==p4) && (p4==p5) && (p5==p6) ) ;

        if( p1 != ivec.end() ) std::cout << "found " << *p1 << '\n' ;
        else std::cout << "not found\n";
    }
}

http://coliru.stacked-crooked.com/a/4464abfe1d4d3540
JLBorges,

Thanks for introducing me to placeholders, bind, and that cool for statement. What I wanted to do was use the function objects in <functional> so that I would not have to write the predicate when it is already written. See p6.
Thank you again.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//	C21.cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

int main()
{
	const std::vector<int> ivec = {2, 9, 7, 4, 5, 6, 3, 8, 1 };
	using std::placeholders::_1;

	for (int value : {3, -7}) {
		const auto p1 = std::find_if(ivec.begin(), ivec.end(), std::bind(std::equal_to < int > {}, _1, value));

		const auto p3 = std::find_if(ivec.begin(), ivec.end(), std::bind(std::equal_to < > {}, _1, value));

		const auto p5 = std::find_if(ivec.begin(), ivec.end(), [value](int v) {return v == value; });

		const auto p6 = std::find_if(ivec.begin(), ivec.end(), std::bind(std::greater < > {}, _1, value));
	
		if (p6 != ivec.end()) {
			std::cout << "found " << *p6 << '\n';
		}
		else {
			std::cout << "not found!" << '\n'; 
		}
	}
}
Topic archived. No new replies allowed.