#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
using namespace std;
class MyPrint
{
public:
void operator()(const int &val)
{
cout << val << " ";
}
};
void test01()
{
vector<int> v1{4, 3, 2, 1, 0};
for_each(v1.begin(), v1.end(), [&](const int &val) {
cout << val << " ";
});
cout << endl;
sort(v1.begin(), v1.end());
for_each(v1.begin(), v1.end(), MyPrint());
cout << endl;
}
class TransForm
{
public:
int operator()(int val)
{
return val;
}
};
void test02()
{
vector<int> v1{5, 4, 3, 2, 1, 0};
vector<int> v2;
v2.resize(v1.size());
transform(v1.begin(), v1.end(), v2.begin(), TransForm());
for_each(v2.begin(), v2.end(), MyPrint());
cout << endl;
}
void test03()
{
vector<int> v1{0, 1, 2, 3, 4};
vector<int>::iterator it = find(v1.begin(), v1.end(), 2);
if (it == v1.end())
{
cout << "没找到!" << endl;
}
else
{
cout << "找到:" << *it << endl;
}
it = find_if(v1.begin(), v1.end(), [&](int &val) -> bool {
return val > 2;
});
if (it == v1.end())
{
cout << "没找到大于2的数!" << endl;
}
else
{
cout << "找到第一个大于2的数:" << *it << endl;
}
vector<char> v2{'A', 'B', 'C', 'D', 'D', 'E'};
auto it2 = adjacent_find(v2.begin(), v2.end());
if (it2 == v2.end())
{
cout << "没有重复的元素!" << endl;
}
else
{
cout << "第一个重复的元素是:" << *it2 << endl;
}
vector<int> v3{2, 0, 2, 3, 1};
sort(v3.begin(), v3.end());
bool ret = binary_search(v3.begin(), v3.end(), 2);
if (ret)
{
int num = count(v3.begin(), v3.end(), 2);
cout << "有" << num << "个元素2!" << endl;
}
else
{
cout << "没有元素2!" << endl;
}
int num = count_if(v3.begin(), v3.end(), [&](int &val) -> bool {
return val > 2;
});
cout << "大于2的个数:" << num << endl;
}
void test04()
{
vector<int> v1{2, 5, 3, 0, 1};
sort(v1.begin(), v1.end());
for_each(v1.begin(), v1.end(), MyPrint());
cout << endl;
sort(v1.begin(), v1.end(), greater<int>());
for_each(v1.begin(), v1.end(), MyPrint());
cout << endl;
srand(static_cast<unsigned int>(time(nullptr)));
random_shuffle(v1.begin(), v1.end());
for_each(v1.begin(), v1.end(), MyPrint());
cout << endl;
vector<int> v2{8, 10, 7, 9};
sort(v2.begin(), v2.end(), greater<int>());
sort(v1.begin(), v1.end(), greater<int>());
vector<int> vTarget;
vTarget.resize(v1.size() + v2.size());
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin(), greater<int>());
for_each(vTarget.begin(), vTarget.end(), MyPrint());
cout << endl;
reverse(vTarget.begin(), vTarget.end());
for_each(vTarget.begin(), vTarget.end(), MyPrint());
cout << endl;
}
void test05()
{
vector<int> v1{0, 1, 2, 3, 4};
vector<int> v2;
v2.resize(v1.size());
copy(v1.begin(), v1.end(), v2.begin());
for_each(v2.begin(), v2.end(), MyPrint());
cout << endl;
replace(v1.begin(), v1.end(), 1, 10);
for_each(v1.begin(), v1.end(), MyPrint());
cout << endl;
replace_if(v1.begin(), v1.end(), [&](int &val) {
return val < 3;
}, 0);
for_each(v1.begin(), v1.end(), MyPrint());
cout << endl;
swap(v1, v2);
for_each(v1.begin(), v1.end(), MyPrint());
cout << endl;
}
void test06()
{
vector<int> v1{0, 1, 2, 3, 4};
int sum = accumulate(v1.begin(), v1.end(), 0);
cout << "sum = " << sum << endl;
vector<char> v2;
v2.resize(10);
fill(v2.begin(), v2.end(), 'X');
for_each(v2.begin(), v2.end(), [&](char &val) {
cout << val << " ";
});
cout << endl;
}
void test07()
{
vector<int> v1{0, 1, 2, 3, 4};
vector<int> v2{3, 4, 5, 6, 7};
vector<int> v3;
v3.resize(min(v1.size(), v2.size()));
vector<int>::iterator it1 = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
for_each(v3.begin(), v3.end(), MyPrint());
cout << endl;
vector<int> v4;
v4.resize(v1.size() + v2.size());
auto it2 = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v4.begin());
for_each(v4.begin(), v4.end(), MyPrint());
cout << endl;
vector<int> v5;
v5.resize(max(v1.size(), v2.size()));
auto it3 = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v5.begin());
for_each(v5.begin(), v5.end(), MyPrint());
cout << endl;
}
int main(int argc, char const *argv[])
{
test01();
test02();
test03();
test04();
test05();
test06();
test07();
return 0;
}