find_if

昨日、書いた関数オブジェクトを使うと、argorithmのfind_ifが使えます。

以下は、例です。

#include <iostream>
using namespace std;

template<class T> struct Less_than{
	T value;
	Less_than(const T& v) : value(v){}
	bool operator()(const T& v) const { return v<value; }
};

int main(){
	int array[5];
	for(int i = 0; i < 5; i++){
		array[i] = 5 - i; // 5, 4, 3, 2, 1
	}

	Less_than<double> f(3.14);

	int* first = array;
	int* last = array + 5;
	int* p = find_if(first, last, f);

	if(p){
		cout << "found! : " << *p << endl; // -> found! : 3 
	}
	else{
		cout << "not found" << endl;
	}

	return 0;
}