1 函数对象
重载函数调用操作符的类,其对象常称为函数对象,即它们是行为类似函数的对象,
也叫仿函数,其实就是重载“()”操作符,使得类对象可以像函数那样调用。
1.1 本质是一个类的对象,因此称为函数对象,也叫仿函数
1.2 函数对象 超出了普通函数的概念,可以拥有自己状态
1.3 函数对象可以作为函数参数
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class MyPrint
{
public:
void operator()(int num)
{
cout << num << endl;
m_Count++;
}
int m_Count = 0;
};
void myPrint02(int num)
{
cout << num << endl;
}
void test01()
{
MyPrint myPrint;
myPrint(100);
myPrint02(100);
}
void test02()
{
MyPrint myPrint;
myPrint(100);
myPrint(100);
myPrint(100);
myPrint(100);
cout << "调用次数为: " << myPrint.m_Count << endl;
}
void doPrint(MyPrint myPrint , int num)
{
myPrint(num);
}
void test03()
{
doPrint(MyPrint(), 1000);
}
int main(){
test03();
system("pause");
return EXIT_SUCCESS;
}
2 谓词
2.1 普通函数或者仿函数的返回值是bool类型,称为谓词
2.2 一元谓词
2.2.1 查找容器中大于20的数字 find_if
2.3 二元谓词
2.3.1 对容器进行排序 sort
2.4 lambda表达式 [](){}
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
class GreaterThan20
{
public:
bool operator()(int val)
{
return val > 20;
}
};
void test01()
{
vector<int>v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);
vector<int>::iterator ret = find_if(v.begin(), v.end(), GreaterThan20());
if ( ret != v.end())
{
cout << "找到大于20的数字为: " << *ret << endl;
}
else
{
cout << "未找到" << endl;
}
}
void myPrintInt( int val)
{
cout << val << " ";
}
class MyCompare
{
public:
bool operator()(int v1,int v2)
{
return v1 > v2;
}
};
void test02()
{
vector<int>v;
v.push_back(10);
v.push_back(30);
v.push_back(20);
v.push_back(40);
v.push_back(50);
sort(v.begin(), v.end());
for_each(v.begin(), v.end(), myPrintInt);
cout << endl;
sort(v.begin(), v.end(), MyCompare());
for_each(v.begin(), v.end(), [](int val){ cout << val << " "; });
cout << endl;
}
int main(){
test02();
system("pause");
return EXIT_SUCCESS;
}
3 内建函数对象
3.1 引入头文件 #include< functional>
3.2 取反 negate<int>
3.3 加法 plus<int>
3.4 大于 greater<int>
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <functional>
#include <vector>
#include <algorithm>
void test01()
{
negate<int>n;
cout << n(10) << endl;
}
void test02()
{
plus<int> p;
cout << p(10, 10) << endl;
}
void test03()
{
vector<int>v;
v.push_back(20);
v.push_back(50);
v.push_back(10);
v.push_back(30);
v.push_back(40);
sort(v.begin(), v.end(), greater<int>());
for_each(v.begin(), v.end(), [](int val){cout << val << " "; });
cout << endl;
}
int main(){
test03();
system("pause");
return EXIT_SUCCESS;
}
4 适配器
4.1 函数对象适配器
4.1.1 //1、利用bind2nd 进行绑定
4.1.2 //2、继承 public binary_function<参数1 类型,参数2类型,返回值类型>
4.1.3 //3、加const
4.2 取反适配器
4.2.1 一元取反 not1
4.2.1.1 //1、利用not1进行取反
4.2.1.2 //2、继承 public unary_function<int,bool>
4.2.1.3 //3、加const
4.2.2 二元取反 not2
4.3 函数指针适配器
4.3.1 ptr_fun将普通函数指针 适配成函数对象
4.4 成员函数适配器
4.4.1 如果存放的是对象实体 mem_fun_ref
4.4.2 如果存放的是对象指针 mem_fun
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <functional>
#include <string>
class MyPrint :public binary_function<int,int,void>
{
public:
void operator()(int val , int start)const
{
cout << "val = " << val << " start = " << start << " sum = " <<val + start << endl;
}
};
void test01()
{
vector<int>v;
for (int i = 0; i < 10;i++)
{
v.push_back(i);
}
cout << "请输入起始累加值: " << endl;
int num;
cin >> num;
for_each(v.begin(), v.end(), bind2nd( MyPrint(), num ) );
}
class GreaterThanFive:public unary_function<int,bool>
{
public:
bool operator()(int val) const
{
return val > 5;
}
};
void test02()
{
vector<int>v;
for (int i = 0; i < 10;i++)
{
v.push_back(i);
}
vector<int>::iterator pos = find_if(v.begin(), v.end(), not1( bind2nd( greater<int>() , 5 )));
if (pos != v.end())
{
cout << "找到小于5的值为: " << *pos << endl;
}
else
{
cout << "未找到" << endl;
}
sort(v.begin(), v.end(), not2 (less<int>()));
for_each(v.begin(), v.end(), [](int val){cout << val << endl; });
}
void myPrint3( int val , int start)
{
cout << val + start << endl;
}
void test03()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
for_each(v.begin(), v.end(), bind2nd(ptr_fun(myPrint3), 1000));
}
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
void showPerson()
{
cout << "成员函数----姓名: " << this->m_Name << " 年龄: " << this->m_Age << endl;
}
void addAge()
{
this->m_Age += 100;
}
string m_Name;
int m_Age;
};
void test04()
{
vector< Person > v;
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
for_each(v.begin(), v.end(), mem_fun_ref(&Person::showPerson));
for_each(v.begin(), v.end(), mem_fun_ref(&Person::addAge));
for_each(v.begin(), v.end(), mem_fun_ref(&Person::showPerson));
}
int main(){
test04();
system("pause");
return EXIT_SUCCESS;
}
5 常用遍历算法
5.1 for_each
5.1.1 用于遍历
5.1.2 有返回值
5.1.3 可以绑定参数进行输出
5.2 transform
5.2.1 搬运
5.2.2 注意:目标容器要有容量
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <functional>
class MyPrint
{
public:
void operator()(int val)
{
cout << val << endl;
m_Count++;
}
int m_Count = 0;
};
void test01()
{
vector<int>v;
for (int i = 0; i < 10;i++)
{
v.push_back(i);
}
MyPrint print = for_each(v.begin(), v.end(), MyPrint());
cout << "print.count = " << print.m_Count << endl;
}
class MyPrint2 :public binary_function<int,int,void>
{
public:
void operator()(int val , int start) const
{
cout << val << endl;
}
};
void test02()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
for_each(v.begin(), v.end(), bind2nd( MyPrint2(), 1000));
}
class MyTransform
{
public:
int operator()(int val)
{
return val + 10000;
}
};
void test03()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
vector<int>v2;
v2.resize(v.size());
transform(v.begin(), v.end(), v2.begin(), MyTransform());
for_each(v2.begin(), v2.end(), [](int val){cout << val << " "; });
}
int main(){
test03();
system("pause");
return EXIT_SUCCESS;
}
6 常用查找算法
6.1 find 查找
6.2 find_if 按条件查找
6.3 adjacent_find算法 查找相邻重复元素
6.4 binary_search算法 二分查找法
6.4.1 注意: 在无序序列中不可用
6.5 count算法 统计元素出现次数
6.6 count_if 按条件进行统计
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <string>
#include <functional>
void test01()
{
vector<int>v;
for (int i = 0; i < 10;i++)
{
v.push_back(i);
}
vector<int>::iterator pos = find(v.begin(), v.end(), 5);
if (pos != v.end())
{
cout << "找到了元素:" << *pos << endl;
}
else
{
cout << "未找到" << endl;
}
}
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
bool operator==(const Person & p)
{
return this->m_Name == p.m_Name && this->m_Age == p.m_Age;
}
string m_Name;
int m_Age;
};
void test02()
{
vector<Person> v;
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
vector<Person>::iterator pos = find(v.begin(), v.end(), p2);
if (pos != v.end())
{
cout << "找到了元素 姓名: " << (*pos).m_Name << " 年龄: " << (*pos).m_Age << endl;
}
}
class MyComparePerson :public binary_function< Person *, Person *, bool>
{
public:
bool operator()( Person * p1 , Person *p2 ) const
{
return p1->m_Name == p2->m_Name && p1->m_Age == p2->m_Age;
}
};
void test03()
{
vector<Person *> v;
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
v.push_back(&p1);
v.push_back(&p2);
v.push_back(&p3);
v.push_back(&p4);
Person * p = new Person("bbb", 20);
vector<Person *>::iterator pos = find_if(v.begin(), v.end(), bind2nd( MyComparePerson() ,p) );
if (pos != v.end())
{
cout << "找到了元素--- 姓名: " << (*pos)->m_Name << " 年龄: " << (*pos)->m_Age << endl;
}
else
{
cout << "未找到" << endl;
}
}
void test04()
{
vector<int>v;
v.push_back(3);
v.push_back(2);
v.push_back(300);
v.push_back(300);
v.push_back(6);
v.push_back(3);
vector<int>::iterator ret = adjacent_find(v.begin(), v.end());
if (ret != v.end())
{
cout << "找到了相邻的重复元素: " << *ret << endl;
}
else
{
cout << "未找到" << endl;
}
}
void test05()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
bool ret=binary_search(v.begin(), v.end(), 2);
if (ret)
{
cout << "查到了数据2" << endl;
}
else
{
cout << "未找到数据2" << endl;
}
}
class GreaterThan3
{
public:
bool operator()(int val)
{
return val >= 3;
}
};
void test06()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
v.push_back(3);
v.push_back(3);
v.push_back(3);
int num = count(v.begin(), v.end(), 3);
cout << "3的个数为: " << num << endl;
num = count_if(v.begin(), v.end(), GreaterThan3());
cout << "大于等于3的个数为: " << num << endl;
}
int main(){
test06();
system("pause");
return EXIT_SUCCESS;
}
7 常用排序算法
7.1 merge 合并
7.1.1 将两个容器合并到 目标容器中
7.1.2 注意: 两个容器必须是有序序列
7.1.3 目标容器必须有容量
7.2 sort 排序
7.3 random_shuffle 洗牌
7.4 reverse 反转
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <functional>
#include <ctime>
void test01()
{
vector<int>v1;
vector<int>v2;
for (int i = 0; i < 10;i++)
{
v1.push_back(i);
v2.push_back(i + 1);
}
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(), [](int val){cout << val << " "; });
}
void test02()
{
vector<int>v;
for (int i = 0; i < 10;i++)
{
v.push_back(i);
}
sort(v.begin(), v.end(), greater<int>());
for_each(v.begin(), v.end(), [](int val){cout << val << " "; });
cout << endl;
}
void test03()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), [](int val){cout << val << " "; });
cout << endl;
}
void test04()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
cout << "反转前打印:" << endl;
for_each(v.begin(), v.end(), [](int val){cout << val << " "; });
cout << endl;
reverse(v.begin(), v.end());
cout << "反转后打印: " << endl;
for_each(v.begin(), v.end(), [](int val){cout << val << " "; });
cout << endl;
}
int main(){
srand((unsigned int)time(NULL));
test04();
system("pause");
return EXIT_SUCCESS;
}
8 常用的拷贝和替换算法
8.1 copy 拷贝
8.1.1 实现打印 copy(v.begin(),v.end() , ostream_iterator<int>(cout , “ ”));
8.2 replace 替换
8.3 replace_if 按条件替换
8.4 swap 交换
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <iterator>
void test01()
{
vector<int>v;
for (int i = 0; i < 10;i++)
{
v.push_back(i);
}
vector<int>v2;
v2.resize(v.size());
copy(v.begin(), v.end(), v2.begin());
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
class MyReplace
{
public:
bool operator()(int val)
{
return val > 3;
}
};
void test02()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
replace(v.begin(), v.end(), 3, 3000);
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << endl;
replace_if(v.begin(), v.end(), MyReplace() , 30000);
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
void test03()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
vector<int>v2(10, 100);
cout << "交换数据前:" << endl;
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << endl;
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "交换数据后:" << endl;
swap(v, v2);
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << endl;
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
int main(){
test03();
system("pause");
return EXIT_SUCCESS;
}
9 常用的算数生成算法
9.1 头文件 #include <numeric>
9.2 accumulate算法 计算容器元素累计总和
9.3 fill算法 向容器中添加元素
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <functional>
#include <numeric>
void test01()
{
vector<int>v;
for (int i = 0; i <= 100;i++)
{
v.push_back(i);
}
int num = accumulate(v.begin(), v.end(),1000);
cout << "num = " << num << endl;
}
void test02()
{
vector<int>v;
v.resize(10);
fill(v.begin(), v.end(), 100);
for_each(v.begin(), v.end(), [](int val){cout << val << " "; });
cout << endl;
}
int main(){
test02();
system("pause");
return EXIT_SUCCESS;
}
10 常用集合算法
10.1 set_intersection算法 求两个set集合的交集
10.2 set_union算法 求两个set集合的并集
10.3 set_difference算法 求两个set集合的差集
10.4 注意:两个集合必须是有序序列
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
void test01()
{
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, [](int val){cout << val << " "; });
cout << endl;
}
void test02()
{
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(v1.size() + v2.size());
vector<int>::iterator itEnd = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
for_each(vTarget.begin(), itEnd , [](int val){cout << val << " "; });
cout << endl;
}
void test03()
{
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( max(v1.size(),v2.size()) );
vector<int>::iterator itEnd = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), vTarget.begin());
for_each(vTarget.begin(), itEnd, [](int val){cout << val << " "; });
cout << endl;
}
int main(){
test03();
system("pause");
return EXIT_SUCCESS;
}