#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <numeric>
#include <string>
#include <ctime>
using namespace std;
//1、常用遍历算法
// for_each遍历
void print01(int val) {
cout << val << " ";
}
class print02 {
public:
void operator()(int val) {
cout << val << " ";
}
};
void test01() {
vector<int> v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
// 普通函数
for_each(v.begin(), v.end(), print01);
cout << endl;
// 仿函数
for_each(v.begin(), v.end(), print02());
cout << endl;
}
// transform
int change(int val) {
return val * 10;
}
class Transform {
public:
int operator()(int val) {
return val;
}
};
void test02() {
vector<int> v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
for_each(v.begin(), v.end(), print01);
cout << endl;
vector<int> v2;
// 开辟空间
v2.resize(v.size());
//源容器开始迭代器 源容器结束迭代器 目标容器开始迭代器 函数或者函数对象
// transform(v.begin(), v.end(), v2.begin(), Transform());
transform(v.begin(), v.end(), v2.begin(), change);
for_each(v2.begin(), v2.end(), print01);
cout << endl;
}
//2、常用查找算法
// 常用查找算法 find
// 查找内置
void test03() {
vector<int> v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
vector<int>::iterator it = find(v.begin(), v.end(), 56);
if (it == v.begin()) {
cout << "没有找到元素" << endl;
} else {
cout << "找到了元素:" << *it << endl;
}
}
// 查找自定义
class Person {
public:
Person(string name, int age) {
this->m_Name = name;
this->m_Age = age;
}
// 重载== 底层find知道如何对比person数据类型
bool operator==(const Person &p) {
if (this->m_Name == p.m_Name && this->m_Age == p.m_Age) {
return true;
} else {
return false;
}
}
string m_Name;
int m_Age;
};
void printPerson(Person p) {
cout << "姓名:" << p.m_Name << " 年龄:" << p.m_Age << endl;
}
void test04() {
vector<Person> v;
Person p1("aa", 11);
Person p2("bb", 12);
v.push_back(p1);
v.push_back(p2);
for_each(v.begin(), v.end(), printPerson);
vector<Person>::iterator it = find(v.begin(), v.end(), p2);
if (it != v.begin()) {
cout << "找到了" << endl;
printPerson(*it);
} else {
cout << "没有找到" << endl;
}
}
// 查找find_if 内置数据类型
class GreaterFive {
public:
bool operator()(int val) {
return val > 5;
}
};
void test05() {
vector<int> v1;
for (int i = 0; i < 10; ++i) {
v1.push_back(i);
}
// 开始迭代器 结束迭代器 _Pred函数或者谓词(返回bool类型的仿函数)
vector<int>::iterator pos = find_if(v1.begin(), v1.end(), GreaterFive());
if (pos == v1.end()) {
cout << "没有找到" << endl;
} else {
cout << "找到了" << *pos << endl;
}
}
// 查找find_if 自定义数据类型
class Person2 {
public:
Person2(string name, int age) {
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
void printPerson2(Person2 p) {
cout << "姓名:" << p.m_Name << " 年龄:" << p.m_Age << endl;
}
class Greater20 {
public:
bool operator()(Person2 &p) {
return p.m_Age > 20;
}
};
void test06() {
vector<Person2> v;
Person2 p1("aa", 10);
Person2 p2("bb", 20);
Person2 p3("cc", 30);
Person2 p4("dd", 40);
Person2 p5("ee", 50);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
for_each(v.begin(), v.end(), printPerson2);
vector<Person2>::iterator it = find_if(v.begin(), v.end(), Greater20());
if (it != v.begin()) {
cout << "找到了" << endl;
printPerson2(*it);
} else {
cout << "没有找到" << endl;
}
}
// 常用查找算法 adjacent_find
void pirntInt(int val) {
cout << val << " ";
}
void test07() {
vector<int> v;
v.push_back(0);
v.push_back(2);
v.push_back(0);
v.push_back(3);
v.push_back(1);
v.push_back(4);
v.push_back(3);
v.push_back(3);
vector<int>::iterator pos = adjacent_find(v.begin(), v.end());
if (pos != v.end()) {
cout << "找到了" << endl;
for_each(pos, v.end(), pirntInt);
cout << endl;
} else {
cout << "位找到" << endl;
}
}
//binary_search 二分查找
void test08() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
v.push_back(2);
// 注意:容器必须是有序的序列,如果是无序序列,结果未知
int ret = binary_search(v.begin(), v.end(), 9);
if (ret) {
cout << "找到了" << endl;
} else {
cout << "没有找到" << endl;
}
}
//常用查找算法 count 统计内置数据类型
void test09() {
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(1);
v.push_back(3);
// 开始迭代器 结束迭代器 统计的元素
cout << count(v.begin(), v.end(), 1) << endl;
}
//常用查找算法 count 统计自定义数据类型
class Person3 {
public:
Person3(string name, int age) {
this->m_Name = name;
this->m_Age = age;
}
// 重载== 底层find知道如何对比Person3数据类型
bool operator==(const Person3 &p) {
if (this->m_Name == p.m_Name && this->m_Age == p.m_Age) {
return true;
} else {
return false;
}
}
string m_Name;
int m_Age;
};
void test10() {
// 创建一个vector容器,数组
vector<Person3> v;
//向容器中插入数据
Person3 p1("aa", 1);
Person3 p2("a2", 2);
Person3 p3("a3", 3);
Person3 p4("aa", 1);
Person3 p5("a5", 5);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
Person3 p("aa", 1);
cout << count(v.begin(), v.end(), p) << endl;
}
//常用的查找泛 count_if 统计内置数据类型
class Greater19 {
public:
bool operator()(int val) {
return val > 19;
}
};
void test11() {
vector<int> v;
v.push_back(10);
v.push_back(40);
v.push_back(30);
v.push_back(20);
v.push_back(40);
v.push_back(20);
cout << count_if(v.begin(), v.end(), Greater19()) << endl;
}
//常用的查找泛 count_if 统计自定义数据类型
class Person4 {
public:
Person4(string name, int age) {
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
class AgeGreater20 {
public:
bool operator()(const Person4 &p) {
return p.m_Age > 20;
}
};
void test12() {
// 创建一个vector容器,数组
vector<Person4> v;
//向容器中插入数据
Person4 p1("aa", 1);
Person4 p2("a2", 20);
Person4 p3("a3", 30);
Person4 p4("aa", 10);
Person4 p5("a5", 50);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
cout << count_if(v.begin(), v.end(), AgeGreater20()) << endl;
}
//3、常用排序算法
//常用排序算法sort
void myPrint(int val) {
cout << val << " ";
}
void test13() {
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(50);
v.push_back(20);
v.push_back(40);
v.push_back(60);
// 从小到大
sort(v.begin(), v.end());
for_each(v.begin(), v.end(), myPrint);
cout << endl;
// 从大到小
sort(v.begin(), v.end(), greater<int>());
for_each(v.begin(), v.end(), myPrint);
cout << endl;
}
//常用排序算法 random_shuffle 指定范围内元素的顺序打乱
void test14() {
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(50);
v.push_back(20);
v.push_back(40);
v.push_back(60);
// 打乱次序
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), myPrint);
cout << endl;
}
//常用排序算法 merge
void test15() {
vector<int> v1;
vector<int> v2;
for (int i = 0; i < 10; ++i) {
v1.push_back(i);
v2.push_back(i + 1);
}
for_each(v1.begin(), v1.end(), myPrint);
cout << endl;
for_each(v2.begin(), v2.end(), myPrint);
cout << endl;
vector<int> vTarget;
// 提前准备空间
vTarget.resize(v1.size() + v2.size());
// 合并两个有序序列
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
for_each(vTarget.begin(), vTarget.end(), myPrint);
}
//常用排序算法reverse
void test16() {
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(50);
v.push_back(20);
v.push_back(40);
v.push_back(60);
for_each(v.begin(), v.end(), myPrint);
cout << endl;
// 反转元素
reverse(v.begin(), v.end());
for_each(v.begin(), v.end(), myPrint);
cout << endl;
}
//4、常用拷贝和替换算法
// 常用拷贝和替换算法 copy
void test17() {
vector<int> v1;
for (int i = 0; i < 10; ++i) {
v1.push_back(i);
}
vector<int> v2;
v2.resize(v1.size());
// 拷贝元素
copy(v1.begin(), v1.end(), v2.begin());
for_each(v2.begin(), v2.end(), myPrint);
cout << endl;
}
//常用拷贝和替换算法 replace
void test18() {
vector<int> v;
v.push_back(20);
v.push_back(30);
v.push_back(50);
v.push_back(40);
v.push_back(20);
v.push_back(10);
v.push_back(20);
replace(v.begin(), v.end(), 20, 200);
for_each(v.begin(), v.end(), myPrint);
cout << endl;
}
//常用拷贝和替换算法 replace_if
class GreaterTen {
public:
bool operator()(int val) {
return val > 10;
}
};
void test19() {
vector<int> v;
v.push_back(20);
v.push_back(30);
v.push_back(5);
v.push_back(40);
v.push_back(20);
v.push_back(10);
v.push_back(20);
replace_if(v.begin(), v.end(), GreaterTen(), 200);
for_each(v.begin(), v.end(), myPrint);
cout << endl;
}
//常用拷贝和替换算法 swap
void test20() {
vector<int> v1;
vector<int> v2;
for (int i = 0; i < 10; ++i) {
v1.push_back(i);
v2.push_back(i + 100);
}
cout << "交换前:" << endl;
for_each(v1.begin(), v1.end(), myPrint);
cout << endl;
for_each(v2.begin(), v2.end(), myPrint);
cout << endl;
swap(v1, v2);
cout << "交换后:" << endl;
for_each(v1.begin(), v1.end(), myPrint);
cout << endl;
for_each(v2.begin(), v2.end(), myPrint);
cout << endl;
}
//5、常用算术生成算法
// 常用算术生成算法 accumulate
void test21() {
vector<int> v1;
for (int i = 0; i <= 100; ++i) {
v1.push_back(i);
}
// 计算容器累加总和
cout << "total=" << accumulate(v1.begin(), v1.end(), 0) << endl;
}
//常用算术生成算法 fill
void test22() {
vector<int> v1;
v1.resize(10);
// 填充
fill(v1.begin(), v1.end(), 100);
// 计算容器累加总和
for_each(v1.begin(), v1.end(), myPrint);
}
//6、常用集合算法
void test23() {
// 有序序列
vector<int> v1;
vector<int> v2;
for (int i = 0; i < 10; ++i) {
v1.push_back(i);
v2.push_back(i + 5);
}
vector<int> vTarget;
vTarget.resize(min(v1.size(), v2.size()));
// 两个容器的交集
vector<int>::iterator itEnd = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
for_each(vTarget.begin(), itEnd, myPrint);
cout << endl;
vector<int> v2Target;
v2Target.resize(plus<int>()(v1.size(), v2.size()));
// 两个容器的并集
vector<int>::iterator itEnd2 = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v2Target.begin());
for_each(v2Target.begin(), itEnd2, myPrint);
cout << endl;
vector<int> v3Target;
v3Target.resize(max(v1.size(), v2.size()));
// 两个容器的差集
vector<int>::iterator itEnd3 = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v3Target.begin());
for_each(v3Target.begin(), itEnd3, myPrint);
cout << endl;
vector<int> v4Target;
v4Target.resize(max(v1.size(), v2.size()));
// 两个容器的差集
vector<int>::iterator itEnd4 = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), v4Target.begin());
for_each(v4Target.begin(), itEnd4, myPrint);
cout << endl;
}
int main() {
// 随机数种子
srand((unsigned int) time(NULL));
// test01();
// test02();
// test03();
// test04();
// test05();
// test06();
// test07();
// test08();
// test09();
// test10();
// test11();
// test12();
// test13();
// test14();
// test15();
// test16();
// test17();
// test18();
// test19();
// test20();
// test21();
// test22();
test23();
system("pause");
return 0;
}