第11章 算法
- (11.1) 函数对象
- (11.2) for_each
- (11.3) transform
- (11.4) find
- (11.5) find_if
- (11.6) adjacent_find
- (11.7) binary_search
- (11.8) count
- (11.9) count_if
- (11.10) sort
- (11.11) random_shuffle
- (11.12) merge
- (11.13) reverse
- (11.14) copy
- (11.15) replace
- (11.16) replace_if
- (11.17) swap
- (11.18) accumulate
- (11.19) fill
- (11.20) set_intersection
- (11.21) set_union
- (11.22) set_difference
(11.1) 函数对象
重载了函数调用操作符的类,其对象称为函数对象。特别地,函数对象使用重载的
()
时,由于其行为类似函数调用,因此也叫仿函数。
#include<iostream>
#include<string>
using namespace std;
class MyAdd
{
public:
int operator()(const int a,const int b)
{
return a + b;
}
};
class MyPrint
{
public:
int count; //记录调用次数
public:
MyPrint()
{
this->count = 0;
}
void operator()(const string str)
{
cout << str << endl;
this->count++;
}
};
void doPrint(MyPrint& mp,string str)
{
mp(str);
}
int main()
{
MyAdd myAdd; //创建函数对象
cout << myAdd(10, 20) << endl; //(1)函数对象可以像普通函数那样调用,可以有参数和返回值
MyPrint myPrint;
myPrint("Hello!");
myPrint("Hello!");
myPrint("Hello!");
cout << "myPrint的调用次数为:" << myPrint.count << endl; //(2)函数对象可以有自己内部的状态(输出3)
doPrint(myPrint,"Hello!"); //(3)函数对象可以作为参数进行传递
system("pause");
return 0;
}
返回
bool
类型的仿函数称为谓词。如果operator()
接受一个参数,那么叫做一元谓词;如果operator()
接受两个参数,那么叫做二元谓词。
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class GT3 //一元谓词
{
public:
bool operator()(const int a)
{
return a > 3;
}
};
class MyCompare //二元谓词
{
public:
bool operator()(const int a,const int b)
{
return a > b; //降序
}
};
int main()
{
vector<int> v;
for (int i = 0;i < 5;i++)
{
v.push_back(i);
}
//find_if算法第三个参数是谓词(即返回值为bool类型的仿函数)
vector<int>::iterator it = find_if(v.begin(),v.end(),GT3()); //查找容器中大于3的数,返回迭代器
if(it == v.end())
{
cout << "没有找到。" << endl;
}
else
{
cout << *it << endl; //4
}
sort(v.begin(),v.end()); //默认升序排列
//sort算法的第三个参数也是谓词
sort(v.begin(),v.end(),MyCompare());
system("pause");
return 0;
}
STL也提供了一些仿函数,这些仿函数被称为内建函数对象。有算术仿函数、关系仿函数、逻辑仿函数。需要包含头文件
#include<functional>
。
#include<iostream>
#include<functional> //使用内建函数对象需要包含这个头文件
using namespace std;
int main()
{
//取相反数仿函数
negate<int> n;
cout << n(10) << endl; //-10
//取余仿函数
modulus<int> md;
cout << md(13,5) << endl;; //3
//加法仿函数
plus<int> p;
cout << p(10,20) << endl; //30
//减法仿函数
minus<int> m;
cout << m(30,10) << endl; //20
//乘法仿函数
multiplies<int> ml;
cout << ml(20,30) << endl; //600
//除法仿函数
divides<int> d;
cout << d(100,10) << endl; //10
system("pause");
return 0;
}
#include<iostream>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;
int main()
{
vector<int> v;
v.push_back(20);
v.push_back(10);
v.push_back(30);
sort(v.begin(),v.end(),greater<int>()); //用STL提供的关系仿函数,实现降序排序
//其他STL关系仿函数:
//equal_to<T>(); //等于
//not_equal_to<T>(); //不等于
//greater<T>(); //大于
//less<T>(); //小于
//greater_equal<T>(); //大于等于
//less_equal<T>(); //小于等于
system("pause");
return 0;
}
#include<iostream>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;
int main()
{
vector<bool> v1;
v1.push_back(true);
v1.push_back(false);
vector<bool> v2;
v2.resize(v1.size());
transform(v1.begin(),v1.end(),v2.begin(),logical_not<bool>()); //将v1搬运到v2中并取反
//其他STL逻辑仿函数:
//logical_and<T>(); //与
//logical_or<T>(); //或
//logical_not<T>(); //非
system("pause");
return 0;
}
(11.2) for_each
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void print(int a)
{
cout << a << " ";
}
class Print
{
public:
void operator()(int a)
{
cout << a << " ";
}
};
int main()
{
vector<int> v;
for (int i = 0;i < 5;i++)
{
v.push_back(i);
}
for_each(v.begin(),v.end(),print); //第三个参数是普通函数
cout << endl;
for_each(v.begin(),v.end(),Print()); //第三个参数是仿函数
cout << endl;
system("pause");
return 0;
}
(11.3) transform
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class Transform
{
public:
int operator()(int a)
{
return a; //啥也没做
}
};
int main()
{
vector<int> v1;
for(int i = 0; i < 5; i++)
{
v1.push_back(i);
}
vector<int> v2;
v2.resize(v1.size());
transform(v1.begin(),v1.end(),v2.begin(),Transform()); //把v1搬运到v2里面去
system("pause");
return 0;
}
(11.4) find
查 找 内 置 数 据 类 型 查找内置数据类型 查找内置数据类型
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
vector<int> v;
for (int i = 0;i < 5;i++)
{
v.push_back(i);
}
vector<int>::iterator pos = find(v.begin(),v.end(),5);
if(pos == v.end())
{
cout << "没有找到。" << endl;
}
else
{
cout << *pos << endl;
}
system("pause");
return 0;
}
查 找 自 定 义 数 据 类 型 查找自定义数据类型 查找自定义数据类型
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
class Person
{
public:
string m_Name;
int m_Age;
public:
Person(string name,int age)
{
this->m_Name = name;
this->m_Age = age;
}
bool operator==(const Person& p) //重载==,定义查找规则
{
if(this->m_Name == p.m_Name && this->m_Age == p.m_Age)
{
return true;
}
else
{
return false;
}
}
};
int main()
{
vector<Person> v;
Person p1("aaa",10);
Person p2("bbb",20);
Person p3("ccc",30);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
vector<Person>::iterator pos = find(v.begin(),v.end(),Person("aaa",10));
if(pos == v.end())
{
cout << "没有找到。" << endl;
}
else
{
cout << "姓名:" << pos->m_Name << "年龄:" << pos->m_Age << endl;
}
system("pause");
return 0;
}
(11.5) find_if
查 找 内 置 数 据 类 型 查找内置数据类型 查找内置数据类型
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class GT5
{
public:
bool operator()(int a)
{
return a > 5;
}
};
int main()
{
vector<int> v;
for (int i = 0; i < 5; i++)
{
v.push_back(i);
}
//find_if算法适合用来查找某个范围的元素
vector<int>::iterator pos = find_if(v.begin(),v.end(),GT5()); //查找>5的数
if (pos == v.end())
{
cout << "没有找到。" << endl;
}
else
{
cout << *pos << endl;
}
system("pause");
return 0;
}
查 找 自 定 义 数 据 类 型 查找自定义数据类型 查找自定义数据类型
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
class Person
{
public:
string m_Name;
int m_Age;
public:
Person(string name,int age)
{
this->m_Name = name;
this->m_Age = age;
}
};
class AgeGT19
{
public:
bool operator()(Person& p)
{
return p.m_Age > 19;
}
};
int main()
{
vector<Person> v;
Person p1("aaa", 18);
Person p2("bbb", 19);
Person p3("ccc", 20);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
vector<Person>::iterator pos = find_if(v.begin(),v.end(),AgeGT19()); //查找年龄大于19的Person
if(pos == v.end())
{
cout << "没有找到。" << endl;
}
else
{
cout << "姓名:" << pos->m_Name << "年龄:" << pos->m_Age << endl;
}
system("pause");
return 0;
}
(11.6) adjacent_find
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
vector<int> v;
v.push_back(20);
v.push_back(10);
v.push_back(10);
v.push_back(30);
vector<int>::iterator pos = adjacent_find(v.begin(),v.end()); //查找相邻的重复元素
if (pos == v.end())
{
cout << "没有找到有相邻的重复元素。" << endl;
}
else
{
cout << *pos << endl; //10
}
system("pause");
return 0;
}
(11.7) binary_search
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
vector<int> v;
for(int i = 0;i < 5;i++)
{
v.push_back(i);
}
bool searchRet = binary_search(v.begin(),v.end(),5); //二分查找是否有5(必须是有序序列)
if (!searchRet)
{
cout << "容器中没有5。" << endl;
}
else
{
cout << "容器中有5。" << endl;
}
system("pause");
return 0;
}
(11.8) count
统 计 内 置 数 据 类 型 统计内置数据类型 统计内置数据类型
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(10);
v.push_back(30);
int num = count(v.begin(),v.end(),10); //统计10的个数
cout << "10的个数为:" << num << endl; //2
system("pause");
return 0;
}
统 计 自 定 义 数 据 类 型 统计自定义数据类型 统计自定义数据类型
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
class Person
{
public:
string m_Name;
int m_Age;
public:
Person(string name,int age)
{
this->m_Name = name;
this->m_Age = age;
}
/*
这里必须要加const,底层是这样要求的,
所以以后写重载的时候可以养成习惯统一都加const
*/
bool operator==(const Person& p)
{
if(this->m_Age == p.m_Age)
{
return true;
}
else
{
return false;
}
}
};
int main()
{
vector<Person> v;
Person p1("张三",18);
Person p2("李四",19);
Person p3("王五",18);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
int num = count(v.begin(),v.end(),Person("孙七",18)); //统计和孙七同岁数的人
cout << "和孙七同岁数的人有:" << num << endl; //2
system("pause");
return 0;
}
(11.9) count_if
统 计 内 置 数 据 类 型 统计内置数据类型 统计内置数据类型
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class GT20
{
public:
bool operator()(const int a)
{
return a > 20;
}
};
int main()
{
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
//count_if算法一般是用来统计一个范围
int num = count_if(v.begin(),v.end(),GT20()); //统计>20的元素个数
cout << "大于20的元素个数为:" << num << endl;
system("pause");
return 0;
}
统 计 自 定 义 数 据 类 型 统计自定义数据类型 统计自定义数据类型
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
class Person
{
public:
string m_Name;
int m_Age;
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
};
class AgeGT18
{
public:
bool operator()(const Person& p)
{
return p.m_Age > 18;
}
};
int main()
{
vector<Person> v;
Person p1("张三", 18);
Person p2("李四", 19);
Person p3("王五", 18);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
int num = count_if(v.begin(),v.end(),AgeGT18()); //统计年龄大于18的Person
cout << "人数大于19的人数为:" << num << endl;
system("pause");
return 0;
}
(11.10) sort
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class MyCompare
{
public:
bool operator()(const int a, const int b)
{
return a > b; //降序
}
};
int main()
{
vector<int> v;
v.push_back(20);
v.push_back(10);
v.push_back(30);
sort(v.begin(),v.end()); //默认升序
sort(v.begin(),v.end(),MyCompare()); //传入仿函数改变排序策略
system("pause");
return 0;
}
(11.11) random_shuffle
#include<iostream>
#include<vector>
#include<algorithm>
#include<ctime>
using namespace std;
int main()
{
srand((unsigned int)time(NULL));
vector<int> v;
for(int i = 0; i < 5; i++)
{
v.push_back(i);
}
random_shuffle(v.begin(),v.end()); //随机洗牌
system("pause");
return 0;
}
(11.12) merge
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
vector<int> v1;
vector<int> v2;
for(int i = 0;i < 5;i++)
{
v1.push_back(i + 1);
v2.push_back(i + 2);
}
vector<int> v;
v.resize(v1.size() + v2.size()); //必须指定目标容器的大小
merge(v1.begin(),v1.end(),v2.begin(),v2.end(),v.begin()); //将v1和v2合并到v中(两个都必须有序)
system("pause");
return 0;
}
(11.13) reverse
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
vector<int> v;
for (int i = 0; i < 5; i++)
{
v.push_back(i);
}
reverse(v.begin(),v.end()); //将容器中元素反转
system("pause");
return 0;
}
(11.14) copy
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
vector<int> v1;
for (int i = 0; i < 5; i++)
{
v1.push_back(i);
}
vector<int> v2;
v2.resize(v1.size());
copy(v1.begin(),v1.end(),v2.begin()); //将v1中的元素拷贝到v2中
system("pause");
return 0;
}
(11.15) replace
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
vector<int> v;
for (int i = 0;i < 5;i++)
{
v.push_back(i);
}
replace(v.begin(),v.end(),3,30); //将v中的3替换为30
system("pause");
return 0;
}
(11.16) replace_if
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class GT3
{
public:
bool operator()(const int a)
{
return a > 3;
}
};
int main()
{
vector<int> v;
for (int i = 0;i < 5;i++)
{
v.push_back(i);
}
//replace_if算法适合用来替换某个范围的元素
replace_if(v.begin(),v.end(),GT3(), 0); //将v中大于3的元素替换为0
system("pause");
return 0;
}
(11.17) swap
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
vector<int> v1;
v1.push_back(10);
v1.push_back(20);
vector<int> v2;
v2.push_back(30);
swap(v1,v2); //将v1和v2的元素交换
system("pause");
return 0;
}
(11.18) accumulate
#include<iostream>
#include<vector>
#include<numeric> //需要添加算术生成算法头文件
using namespace std;
int main()
{
vector<int> v;
for(int i = 0;i < 5;i++)
{
v.push_back(i);
}
int total = accumulate(v.begin(),v.end(),0); //第三个参数是起始的累加值
cout << "total = " << total << endl; //10
system("pause");
return 0;
}
(11.19) fill
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v;
v.resize(10); //v有10个0
fill(v.begin(),v.end(),10); //现在v有10个10
system("pause");
return 0;
}
(11.20) set_intersection
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void myPrint(int a)
{
cout << a << " ";
}
int main()
{
vector<int> v1;
vector<int> v2;
for (int i = 0; i < 5; i++)
{
v1.push_back(i + 1); //1~5
v2.push_back(i + 2); //2~6
}
vector<int> v;
v.resize(min(v1.size(),v2.size())); //min也要包含algorithm头文件
//求v1和v2的交集,返回的是结束迭代器
vector<int>::iterator itEnd = set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),v.begin());
for_each(v.begin(),itEnd,myPrint); //2 3 4 5
system("pause");
return 0;
}
(11.21) set_union
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void myPrint(int a)
{
cout << a << " ";
}
int main()
{
vector<int> v1;
vector<int> v2;
for (int i = 0; i < 5; i++)
{
v1.push_back(i + 1); //1~5
v2.push_back(i + 2); //2~6
}
vector<int> v;
v.resize(v1.size() + v2.size());
//求v1和v2的并集,返回的是结束迭代器
vector<int>::iterator itEnd = set_union(v1.begin(),v1.end(),v2.begin(),v2.end(),v.begin());
for_each(v.begin(),itEnd,myPrint); //1 2 3 4 5 6
system("pause");
return 0;
}
(11.22) set_difference
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void myPrint(int a)
{
cout << a << " ";
}
int main()
{
vector<int> v1;
vector<int> v2;
for (int i = 0;i < 5;i++)
{
v1.push_back(i + 1); //1~5
v2.push_back(i + 2); //2~6
}
vector<int> v;
v.resize(max(v1.size(),v2.size()));
//求v1和v2的差集(在v1中,不在v2中),返回的是结束迭代器
vector<int>::iterator itEnd = set_difference(v1.begin(),v1.end(),v2.begin(),v2.end(),v.begin());
for_each(v.begin(),itEnd,myPrint); //1
system("pause");
return 0;
}
《 C + + S Y N T A X 》 系 列 博 客 创 作 参 考 资 料 来 源 《C++\ SYNTAX》系列博客创作参考资料来源 《C++ SYNTAX》系列博客创作参考资料来源
- 《黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难》@黑马程序员.https://www.bilibili.com.
博 客 创 作 : A i d e n L e e 博客创作:Aiden\ Lee 博客创作:Aiden Lee
特别声明:文章仅供学习参考,转载请注明出处,严禁盗用!