黑马程序员课程学习笔记
常用算法有:遍历、查找、排序、拷贝替换、算数生成、集合
需要头文件:* 算法主要是由头文件<algorithm>
<functional>
<numeric>
组成。
<algorithm>
是所有STL头文件中最大的一个,范围涉及到比较、 交换、查找、遍历操作、复制、修改等等<numeric>
体积很小,只包括几个在序列上面进行简单数学运算的模板函数<functional>
定义了一些模板类,用以声明函数对象。
常用遍历算法
- for_each(v.begin(), v.end(),函数名/函数对象);
第三个参数需要自己写一个函数,如果是全局函数,传入函数名,特别注意,这里的是全局函数,不要随便写在一个类中,除非专门写一个打印类,里面就这一个成员函数,也就是函数对象;如果是函数对象,则传入实例对象或者匿名对象。特别注意括号,区分开函数与函数对象(仿函数),·也要和模板参数区分开,很关键 - transform(v.begin(), v.end(), vTarget.begin(), myTransform());
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
//常用遍历:for_each transform
//1.for_each
void myprint(int&val)
{
cout << val << " ";
}
class myPrint {
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(起始,结束,函数/仿函数)
//1.函数,不要括号,函数名即可
for_each(v.begin(), v.end(), myprint);
cout << endl;
//2.匿名函数对象或者实例化对象
for_each(v.begin(), v.end(), myPrint());
cout << endl;
}
//2.transform
class myTransform {
public:
int operator()(int&val)
{
//返回原值
return val;
}
};
void test02()
{
vector<int>v;//原容器
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
vector<int>vTarget;//目标容器
//必须初始化目标容器的空间,否则报错
vTarget.resize(v.size());
//4个参数:原容器的起始结束迭代器,目标容器起始迭代器,函数/函数对象
transform(v.begin(), v.end(), vTarget.begin(), myTransform());
//利用foreach遍历
for_each(v.begin(), v.end(), myprint);
cout << endl;
for_each(vTarget.begin(), vTarget.end(), myprint);
cout << endl;
}
int main() {
//1.for_each
//test01();
//2.transform
test02();
system("pause");
return 0;
}
常用查找算法
- find(起始迭代器,结束迭代器,查找的值),无论找到否,返回一个迭代器
- find_if(iterator beg, iterator end, 函数/谓词);按条件查找
- adjacent_find(iterator beg, iterator end);查找相邻重复元素
- bool binary_search(iterator beg, iterator end, value); 二分查找,必须是有序的才能查
- count(iterator beg, iterator end, value); 统计元素个数
- count_if(iterator beg, iterator end, _Pred);按条件统计
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
//algorithm是stl中最大的头文件,包含范围广
//numeric体积很小,只包括几个在序列上面进行简单数学运算的模板函数
//functional定义了一些模板类, 用以声明函数对象。
//1.find(起始迭代器,结束迭代器,查找的值),无论找到否,返回一个迭代器
//2.find_if(iterator beg, iterator end, 函数/谓词);按条件查找
//3.adjacent_find(iterator beg, iterator end);查找相邻重复元素
//4.bool binary_search(iterator beg, iterator end, value); 二分查找,必须是有序的才能查
//5.count(iterator beg, iterator end, value); 统计元素个数
//6.count_if(iterator beg, iterator end, _Pred);按条件统计
//自定义一个Person类
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;
};
//1.1find-内置数据类型
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;
}
}
//1.2find-自定义数据类型
void test02()
{
vector<Person>v;
string nameSeed = "ABCDE";
for (int i = 0; i < 5; i++)
{
//设置姓名
string name = "Person_";
name += nameSeed[i];
//年龄
int age = 20 + i;
//有参构造创建Person对象
Person p(name, age);
//将对象存入容器v
v.push_back(p);
}
//开始查找,这里既可以传入实例化对象p,也可以直接匿名对象,
//如果有参构造参数是引用方式可能会有问题,所以没有引用
//Person p("Person_C", 23);
vector<Person>::iterator pos = find(v.begin(), v.end(), Person("Person_C", 22));
if (pos != v.end())
{
cout << "找到" << pos->m_Name<<","<<pos->m_Age << endl;
}
else
{
cout << "没找到" << endl;
}
}
//2.1find_if-内置数据类型
class greaterFive {
public:
bool operator()(int& val)
{
//val>5就返回的真
return val > 5;
}
};
void test03()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//查找大于5
vector<int>::iterator pos = find_if(v.begin(), v.end(), greaterFive());
if (pos != v.end())
{
cout << "找到第一个大于5的数为" << *pos<< endl;
}
else
{
cout << "没找到" << endl;
}
}
//2.2find_if-自定义数据类型
class ageGreater22
{
public:
bool operator()(const Person&p)
{
//年龄大于24返回真
return p.m_Age > 22;
}
};
void test04()
{
vector<Person>v;
string nameSeed = "ABCDE";
for (int i = 0; i < 5; i++)
{
//设置姓名
string name = "Person_";
name += nameSeed[i];
//年龄
int age = 20 + i;
//有参构造创建Person对象
Person p(name, age);
//将对象存入容器v
v.push_back(p);
}
vector<Person>::iterator pos;
pos = find_if(v.begin(), v.end(), ageGreater22());
if (pos != v.end())
{
cout << "找到" << pos->m_Name << "," << pos->m_Age << endl;
}
else
{
cout << "没找到" << endl;
}
}
//3.adjacent_find查找相邻重复元素
void test05()
{
vector<int>v;
v.push_back(0);
v.push_back(1);
v.push_back(0);
v.push_back(2);
v.push_back(2);
vector<int>::iterator pos;
pos = adjacent_find(v.begin(), v.end());
if (pos != v.end())
{
cout << "找到" <<*pos<< endl;
}
else
{
cout << "没找到" << endl;
}
}
//4.binary_search 二分查找法,效率高,但要求必须是有序的
void test06()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
bool ret = binary_search(v.begin(), v.end(), 5);
if (ret)
{
cout << "找到了" << endl;
}
else
{
cout << "没找到" << endl;
}
}
//5.1 count-内置数据类型
void test07()
{
vector<int>v;
for (int i = 0; i < 20; i++)
{
v.push_back(rand() % 10);
}
//打印
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << endl;
//查找元素5的个数
int num = count(v.begin(), v.end(), 5);
cout << "5的个数:" << num << endl;
}
//5.2 count-自定义数据类型
void test08()
{
vector<Person>v;
string nameSeed = "ABCDE";
for (int i = 0; i < 5; i++)
{
//设置姓名
string name = "Person_";
name += nameSeed[i];
//年龄
int age = 20 + i;
//有参构造创建Person对象
Person p(name, age);
//将对象存入容器v
v.push_back(p);
}
//这里看你统计的内容,根据需求在自定义数据类型中重载==
//如果需要统计年龄相等,就operator==(this->age=p.age)
//如果是姓名和年龄,就(姓名==,年龄==)
int num = count(v.begin(), v.end(), Person("Person_A", 20));
cout << "姓名年龄和Person_A,20形同的人员个数" << num << endl;
}
//6.count_if 内置数据类型
void test09()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(rand() % 10);
}
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << endl;
int num = count_if(v.begin(), v.end(), greaterFive());
cout << "大于5的元素个数" << num << endl;
}
//6.2 count_if自定义数据类型,需要提供谓词
void test10()
{
vector<Person>v;
string nameSeed = "ABCDE";
for (int i = 0; i < 5; i++)
{
//设置姓名
string name = "Person_";
name += nameSeed[i];
//年龄
int age = 20 + i;
//有参构造创建Person对象
Person p(name, age);
//将对象存入容器v
v.push_back(p);
}
int num = count_if(v.begin(), v.end(), ageGreater22());
cout << "年龄大于22的人员个数" << num << endl;
}
int main() {
//1.1find-内置数据类型
//test01();
//1.2find-自定义数据类型,需要重载==,让find知道如何比较
//test02();
//2.1find_if-内置数据类型,需要提供谓词
//test03();
//2.2find_if-自定义数据类型,需要提供谓词
//test04();
//3.adjacent_find查找相邻重复元素,必须是相邻的重复,返回重复中的第一个元素的迭代器的位置
//test05();
//4.binary_search 二分查找法,高效,但是必须是有序的才能准确查找
//test06();
//5.1 count内置数据类型
//test07();
//5.1 count自定义数据类型,需要在自定义数据类型中重载operator==
//test08();
//6.1 count_if内置数据类型,需要提供谓词
//test09();
//6.2 count_if自定义数据类型,需要提供谓词
test10();
system("pause");
return 0;
}
常用排序算法
- sort //对容器内元素进行排序
- random_shuffle //洗牌 指定范围内的元素随机调整次序,要每次都打乱的不一样,加srand
- merge // 容器元素合并,并存储到另一容器中
- 注意,两个容器必须是同序的才能合并,
- 同为升序,merge默认升序,
- 同为降序,merge第六个参数要提供降序仿函数
greater<int>()
,这个包含functional即可
- reverse // 反转指定范围的元素
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <ctime>
using namespace std;
//sort //对容器内元素进行排序
//random_shuffle //洗牌 指定范围内的元素随机调整次序,要每次都打乱的不一样,加srand
//merge // 容器元素合并,并存储到另一容器中
//reverse // 反转指定范围的元素
//打印vector容器中的数据
void printVector(const vector<int>&v)
{
vector<int>::const_iterator it = v.begin();
for (;it!=v.end();it++)
{
cout << *it << "\t";
}
cout << endl;
}
void test01()
{
//创建3个容器
vector<int>v1;
vector<int>v2;
for (int i = 0; i < 5; i++)
{
//两个容器存入数据
v1.push_back(9-i);
v2.push_back(5-i);
}
//打印:
cout << "v1:\n";
printVector(v1);
cout << "v2:\n";
printVector(v2);
//创建v3
vector<int>v3;
//merge合并到目标容器v3,且必须初始化v3的空间
v3.resize(v1.size() + v2.size());
//注意,两个容器必须是同序的才能合并,
//1.同为升序,merge默认升序,
//2.同为降序,merge第六个参数要提供降序仿函数greater<int>()
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin(),greater<int>());
cout << "合并得v3:\n";
printVector(v3);
//打乱
random_shuffle(v3.begin(), v3.end());
cout << "v3乱序" << endl;
printVector(v3);
//排序,functional自带,传入匿名函数对象
sort(v3.begin(), v3.end(), greater<int>());
cout << "排序后" << endl;
printVector(v3);
//逆序
reverse(v3.begin(), v3.end());
cout << "逆序后" << endl;
printVector(v3);
}
int main() {
srand((unsigned int)time(NULL));
test01();
system("pause");
return 0;
}
常用拷贝 替换算法
- copy // 容器内指定范围的元素拷贝到另一容器中
- replace // 将容器内指定范围的旧元素修改为新元素
- replace_if // 容器内指定范围满足条件的元素替换为新元素
- swap // 互换两个容器的元素
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//copy // 容器内指定范围的元素拷贝到另一容器中
//replace // 将容器内指定范围的旧元素修改为新元素
//replace_if // 容器内指定范围满足条件的元素替换为新元素
//swap // 互换两个容器的元素
void printVector(const vector<int>& v)
{
vector<int>::const_iterator it = v.begin();
for (;it!=v.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
//copy
void test01()
{
//错误示范,只能把一个容器考到另一空容器,并不能接到后面
vector<int>v1;
vector<int>v2;
for (int i = 0; i < 5; i++)
{
v1.push_back(i);
v2.push_back(i + 5);
}
cout << "v1:" << endl;
printVector(v1);
cout << "v2:" << endl;
printVector(v2);
//把v2数据拷贝到v1后面,v1必须扩容
v1.resize(v1.size() + v2.size());
copy(v2.begin(), v2.end(), v1.begin());
cout << "把v2数据拷贝到v1后面" << endl;
printVector(v1);
//正确用法
cout << "-----------------" << endl;
vector<int>v3;
v3.resize(v2.size());
copy(v2.begin(), v2.end(), v3.begin());
printVector(v3);
}
//replace
void test02()
{
vector<int>v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(20);
v.push_back(20);
cout << "替换前:" << endl;
printVector(v);
//把v中所有的20都替换成2000
replace(v.begin(), v.end(), 20, 200);
cout << "替换后:" << endl;
printVector(v);
}
//replace_if,需要谓词
class replaceLess30 {
public:
bool operator()(const int&val)
{
return val < 30;
}
};
void test03()
{
vector<int>v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(20);
v.push_back(20);
cout << "替换前:" << endl;
printVector(v);
//把v中所有小于20的数都替换成200
replace_if(v.begin(), v.end(), replaceLess30(),200);
cout << "替换后:" << endl;
printVector(v);
}
int main() {
//copy
//test01();
//replace
//test02();
//replace_if 需要谓词
test03();
//swap,交换两个容器中的元素
system("pause");
return 0;
}
常用算数生成算法
需要头文件numeric
- accumulate // 计算容器元素累计总和
- fill // 将容器中指定区间内的元素填充为指定元素
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
//需要头文件numeric
//accumulate // 计算容器元素累计总和
//fill // 将容器中指定区间内的元素填充为指定元素
//1.accumulate
void test01()
{
vector<int>v;
for (int i = 0; i <= 100; i++)
{
//v中存入了0到100
v.push_back(i);
}
//accumulate求和,参数3为sum的起始值,如果是1000,则总和+1000
int sum = accumulate(v.begin(), v.end(), 0);
cout << "v中元素总和:" << sum << endl;
}
//2.fill
void test02()
{
vector<int>v;
for (int i = 0; i <= 10; i++)
{
//v中存入了0到100
v.push_back(i);
}
fill(v.begin(), v.end(), -1);
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << endl;
}
int main() {
//test01();
//fill
test02();
system("pause");
return 0;
}
常用集合算法
注意有些是返回迭代器的,要想不输出0,需要用到该迭代器
-
set_intersection // 求两个容器的交集
注意:
1. 目标容器要先开辟空间,最极端的情况是大的完全包含小的
2. 打印的时候要用函数返回的结束迭代器 -
set_union // 求两个容器的并集,两个容器必须是有序数列
-
set_difference // 求两个容器的差集
注意:- 两个容器必须是有序数列
- 两种情况,v1减去和v2的交集,v2减去和v1的交集
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//set_intersection // 求两个容器的交集
// 注意: 1.目标容器要先开辟空间,最极端的情况是大的完全包含小的
// 2.打印的时候要用函数返回的结束迭代器
//
//set_union // 求两个容器的并集
// 注意:1.两个容器必须是有序数列
//
//set_difference // 求两个容器的差集
//注意:1.两个容器必须是有序数列
// 2.两种情况,v1减去和v2的交集,v2减去和v1的交集
//
//1.set_intersection // 求两个容器的交集
void myPrint(const int& val)
{
cout<<val<<" ";
}
//交集
void test01()
{
vector<int>v1;
vector<int>v2;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);//0~9
v2.push_back(i + 5);//5~14
}
//求交集
//创建目标容器
vector<int>vTarget;
//开辟空间,最极端的情况,大集合完全包含小集合
vTarget.resize(min(v1.size(), v2.size()));
//set_intersection返回交集的end,用一个迭代器去接收
vector<int>::iterator itEnd;
itEnd = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
//这里会打印vTarget中所有的元素,包括没有用到的0
for_each(vTarget.begin(), vTarget.end(), myPrint);
cout << endl;
//只有使用函数提供的迭代器,才能只打印交集的部分
for_each(vTarget.begin(), itEnd, myPrint);
cout << endl;
}
//并集
void test02()
{
vector<int>v1;
vector<int>v2;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);//0~9
v2.push_back(i + 5);//5~14
}
//求并集
//创建目标容器
vector<int>vTarget;
//开辟空间,最极端的情况,两个容器完全没有交集
vTarget.resize(v1.size() + v2.size());
//同样是返回一个迭代器
vector<int>::iterator itEnd;
itEnd = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
//这里会打印vTarget中所有的元素,包括没有用到的0
cout << "完整的目标容器:" << endl;
for_each(vTarget.begin(), vTarget.end(), myPrint);
cout << endl;
//只有使用函数提供的迭代器,才能只打印交集的部分
cout << "v1、v2并集:" << endl;
for_each(vTarget.begin(), itEnd, myPrint);
cout << endl;
}
//差集
void test03()
{
vector<int>v1;
vector<int>v2;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);//0~9
v2.push_back(i + 5);//5~14
}
//求差集
//创建目标容器
vector<int>vTarget;
//开辟空间,最极端的情况,没有交集,差集是自身,就需要两个中最大的那个
vTarget.resize(max(v1.size(), v2.size()));
//set_difference返回交集的end,用一个迭代器去接收
vector<int>::iterator itEnd;
itEnd = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
//这里会打印vTarget中所有的元素,包括没有用到的0
//for_each(vTarget.begin(), vTarget.end(), myPrint);
//cout << endl;
//只有使用函数提供的迭代器,才能只打印只有差集的部分
cout << "v1和v2的差集(v1-∩(v1,v2))" << endl;
for_each(vTarget.begin(), itEnd, myPrint);
cout << endl;
//复用,求v2和v1的差集
itEnd = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), vTarget.begin());
cout << "v2和v1的差集(v2-∩(v2,v1))" << endl;
for_each(vTarget.begin(), itEnd, myPrint);
cout << endl;
}
int main() {
//交集
//test01();
//并集
//test02();
//差集
test03();
system("pause");
return 0;
}