#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
// 奇数の判定
//bool is_odd(int x) {
// return x % 2 > 0;
//}
class is_odd : public unary_function<int, bool>
{
public:
result_type operator() (argument_type k)
{
return (result_type)(k % 2);
}
};
// 誤差の範囲で同じか否かのチェック
//bool Equal(double x, double y) {
// return abs(x-y) < 0.1;
//}
class Equal : public binary_function<double, double, bool>
{
public:
result_type operator() (first_argument_type a, second_argument_type b)
{
return (result_type)((abs(a-b) < 0.1) ? 1 : 0);
}
};
int main()
{
// 初期設定(v)
vector<int> v {4, 2, 2, 3, 4};
cout << "vector v :";
for (auto x : v)
cout << " " << x;
cout << endl;
// 検索
vector<int>::iterator it = find(v.begin(), v.end(), 2);
cout << " find : 2 の検索結果(イテレータが示す値):" << *it << endl;
it = find_if(v.begin(), v.end(), is_odd()); // 単項関数オブジェクト
// it = find_if(v.begin(), v.end(), is_odd); // 関数
// it = find_if(v.begin(), v.end(), [](int x) { return x % 2; }); // ラムダ式
cout << " find_if : 奇数の検索結果(イテレータが示す値):" << *it << endl;
it = find_if_not(v.begin(), v.end(), [](int x) { return x % 2; });
cout << " find_if_not : 偶数の検索結果(イテレータが示す値):" << *it << endl;
it = adjacent_find(v.begin(), v.end(), less<int>());
cout << " adjacent_find : 昇順のシーケンス(イテレータが示す値):" << *it << " " << *(++it) << endl;
it = search_n(v.begin(), v.end(), 2, 2);
cout << " search_n : v 内に 2 個の 2 のシーケンス(イテレータが示す値):" << *it << " " << *(++it) << endl;
// 初期設定(v1, v2, v3)
vector<double> v1 {3.5, 5.01, 4.0, 4.95, 4.05};
cout << "vector v1 :";
for (auto x : v1)
cout << " " << x;
cout << endl;
vector<double> v2 {7.0, 5.0};
cout << "vector v2 :";
for (auto x : v2)
cout << " " << x;
cout << endl;
vector<double> v3 {5.0, 4.0};
cout << "vector v3 :";
for (auto x : v3)
cout << " " << x;
cout << endl;
// 検索
vector<double>::iterator itd = find_first_of(v1.begin(), v1.end(), v2.begin(), v2.end(), Equal()); // 二項関数オブジェクト
// vector<double>::iterator itd = find_first_of(v1.begin(), v1.end(), v2.begin(), v2.end(), Equal); // 関数
// vector<double>::iterator itd = find_first_of(v1.begin(), v1.end(), v2.begin(), v2.end(), [](double x, double y) { return abs(x-y) < 0.1; }); // ラムダ式
cout << " find_first_of : v1 内にある v2 の値(イテレータが示す値):" << *itd << endl;
itd = find_end(v1.begin(), v1.end(), v3.begin(), v3.end(), Equal());
// itd = find_end(v1.begin(), v1.end(), v3.begin(), v3.end(), Equal); // 関数
// itd = find_end(v1.begin(), v1.end(), v3.begin(), v3.end(), [](double x, double y) { return abs(x-y) < 0.1; }); // ラムダ式
cout << " find_end : v1 内にある v3 の最後のシーケンス(イテレータが示す値):" << *itd << " " << *(++itd) << endl;
itd = search(v1.begin(), v1.end(), v3.begin(), v3.end(), Equal());
// itd = search(v1.begin(), v1.end(), v3.begin(), v3.end(), Equal); // 関数
// itd = search(v1.begin(), v1.end(), v3.begin(), v3.end(), [](double x, double y) { return abs(x-y) < 0.1; }); // ラムダ式
cout << " search : v1 内にある v3 と同じ最初のシーケンス(イテレータが示す値):" << *itd << " " << *(++itd) << endl;
return 0;
}