#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
// 奇数の判定
class is_odd : public unary_function<int, bool>
{
public:
result_type operator() (argument_type k)
{
return (result_type)(k % 2);
}
};
int main()
{
vector<int>::iterator it;
// 初期設定
vector<int> v1 {0, 1, 2, 3, 4};
printf("v1 :");
for (auto x : v1)
printf(" %d", x);
printf("\n");
// 2 を 20 で置き換える
printf("**v1 の 2 を 20 で置き換える**\n");
replace(v1.begin(), v1.end(), 2, 20);
printf(" v1 :");
for (auto x : v1)
printf(" %d", x);
printf("\n");
// 奇数を 30 で置き換える
printf("**v1 の 奇数を 30 で置き換える**\n");
replace_if(v1.begin(), v1.end(), is_odd(), 30); // 単項関数オブジェクト
// replace_if(v1.begin(), v1.end(), [](int x){ return x%2 > 0; }, 30); // ラムダ式
printf(" v1 :");
for (auto x : v1)
printf(" %d", x);
printf("\n");
// 再初期設定
printf("**再初期設定**\n");
for (int i1 = 0; i1 < 5; i1++)
v1[i1] = i1 + 1;
printf(" v1 :");
for (auto x : v1)
printf(" %d", x);
printf("\n");
vector<int> v2 {10, 20, 30, 40, 50, 60, 70};
printf(" v2 :");
for (auto x : v2)
printf(" %d", x);
printf("\n");
// 2 を 20 で置き換 v2 にコピー
printf("**v1 の 2 を 20 で置き換え v2 にコピー**\n");
replace_copy(v1.begin(), v1.end(), v2.begin(), 2, 20);
printf(" v1 :");
for (auto x : v1)
printf(" %d", x);
printf("\n");
printf(" v2 :");
for (auto x : v2)
printf(" %d", x);
printf("\n");
// 奇数を 30 で置き換え v2 にコピー
printf("**v1 の 奇数を 30 で置き換え v2 にコピー**\n");
replace_copy_if(v1.begin(), v1.end(), v2.begin(), is_odd(), 30);
// replace_copy_if(v1.begin(), v1.end(), v2.begin(), [](int x){ return x%2 > 0; }, 30);
printf(" v1 :");
for (auto x : v1)
printf(" %d", x);
printf("\n");
printf(" v2 :");
for (auto x : v2)
printf(" %d", x);
printf("\n");
return 0;
}