💪 图像算法工程师,专业从事且热爱图像处理,图像处理专栏更新如下👇:
📝《图像去噪》
📝《超分辨率重建》
📝《语义分割》
📝《风格迁移》
📝《目标检测》
📝《图像增强》
📝《模型优化》
📝《模型实战部署》
📝《图像配准融合》
📝《数据集》
📝《高效助手》
📝《C++》
一、常用算法头文件
算法主要是由头文件#include<algorithm><functional><numeric>
组成。
<algorithm> 是所有STL头文件中最大的一个,范围涉及到比较、 交换、查找、遍历操作、复制、修改等等。
<numeric>体积很小,只包括几个在序列上面进行简单数学运算的模板函数。
<functional>定义了一些模板类,用以声明函数对象。
二、遍历算法
常用的遍历算法接口:
for_each //遍历容器
transform //搬运容器到另一个容器中
2.1 for_each算法遍历
实现遍历容器。
2.1.1 函数原型
函数原型:
for_each(iterator beg,iterator end,func);
参数解析:
遍历算法 遍历容器元素
beg 开始迭代器
end 结束迭代器
func函数或者函数对象
2.1.2 示例代码
for_each算法遍历的示例代码见下:
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
// 普通函数
void print01(int val)
{
cout << val <<" ";
}
// 仿函数
class print02
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
// for_each算法基本用法
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;
}
int main()
{
test01();
system("pause");
return 0;
}
2.1.3 输出
运行上面2.1.2中代码,输出见下:
2.2 transform算法遍历
搬运容器到另一个容器中。
2.2.1 函数原型
函数原型:
transform(iterator begl,iterator end1,iterator beg2,_func);
参数解析:
beg1 源容器开始迭代器
end1 源容器结束迭代器
beg2 目标容器开始迭代器
_func 函数或者函数对象
2.2.2 示例代码
下面示例代码中,将一个容器中的数据转移到另外一个容器中。
注意:搬运的目标容器必须提前开辟空间,否则无法正常搬运。
示例代码见下:
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
// 仿函数
class Transform
{
public:
int operator()(int v)
{
// return v;
return v + 100; // 在搬运过程中可以对数据做处理运算
}
};
class MyPrint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
// 遍历transform算法基本用法
void test01()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
vector<int>vTarget; // 目标容器
vTarget.resize(v.size()); // 目标容器,需要提前开辟空间,否则会报错
// 将v容器中内容转移到vTarget容器中
transform(v.begin(),v.end(),vTarget.begin(),Transform());
// 将vTarget容器中的内容遍历输出
for_each(vTarget.begin(),vTarget.end(),MyPrint());
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
2.2.3 输出
运行上面2.2.2中代码,输出见下:
三、查找算法
查找算法的常用接口见下:
find //查找元素
find_if //按条件查找元素
adjacent_find //查找相邻重复元素
binary_search //二分查找法
count //统计元素个数
count_if //按条件统计元素个数
3.1 find查找算法
查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器end()。
3.1.1 函数原型
函数原型:
find(iterator beg,iterator end,value);
参数解析:
按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
beg 开始迭代器
end 结束迭代器
value 查找的元素
3.1.2 示例代码
在下面示例代码中有查找内置数据类型和自定义数据类型,代码见下:
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <string>
// 常用查找算法find
// 查找 内置数据类型
void test01()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
// 查找容器中是否有5这个元素
vector<int>::iterator it = find(v.begin(),v.end(),5); // 不管找到与否,都会返回迭代器
if (it == v.end())
{
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 test02()
{
cout << "下面是自定义数据类型" << endl;
vector<Person>v;
// 创建数据
Person p1("aaa",10);
Person p2("bbb",20);
Person p3("ccc",30);
Person p4("ddd",40);
Person p5("eee",50);
// 将上面数据存放到vector容器中
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
Person pp("bbb",20);
vector<Person>::iterator it = find(v.begin(),v.end(),pp);
if (it == v.end())
{
cout << "没有找到!" << endl;
}
else
{
cout << "找到元素 姓名:" << it->m_Name << "年龄:" << it->m_Age << endl;
}
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
3.1.3 输出
运行上面3.1.2中代码,输出见下:
3.2 find_if查找算法
3.2.1 函数原型
find_if查找算法的原型见下:
find_if(iterator beg,iterator end,_Pred);
参数解析:
按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
beg 开始迭代器
end 结束迭代器
_Pred 函数或者谓词(返回bool类型的仿函数)
3.2.2 示例代码
find_if条件查找算法的示例代码见下,有内置数据类型和自定义数据类型两种情况,代码见下:
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <string>
// 常用查找算法find_if
// 仿函数
class GreaterFive
{
public:
bool operator()(int val)
{
return val > 5;
}
};
// 查找 内置数据类型
void test01()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
vector<int>::iterator it = find_if(v.begin(),v.end(),GreaterFive());
if (it == v.end())
{
cout << "没有找到" << endl;
}
else
{
cout << "找到大于5的数字为:" << *it << endl;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Person
{
public:
Person(string name,int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
class Greater20
{
public:
bool operator()(Person &p)
{
return p.m_Age > 20;
}
};
// 查找 自定义数据类型
void test02()
{
cout << "下面是自定义数据类型" << endl;
vector<Person>v;
// 创建数据
Person p1("aaa",10);
Person p2("bbb",20);
Person p3("ccc",30);
Person p4("ddd",40);
Person p5("eee",50);
// 将上面数据存放到vector容器中
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
// Person pp("bbb",20);
vector<Person>::iterator it = find_if(v.begin(),v.end(),Greater20());
if (it == v.end())
{
cout << "没有找到!" << endl;
}
else
{
cout << "找到元素 姓名:" << it->m_Name << "年龄:" << it->m_Age << endl;
}
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
3.2.3 输出
运行上面3.2.2中代码,输出见下:
3.3 adjacent_find查找算法
查找相邻的重复元素。
3.3.1 函数原型
adjacent_find(iterator beg, iterator end);
参数解析:
查找相邻重复元素,返回相邻元素的第一个位置的迭代器
beg 开始迭代器
end 结束迭代器
3.3.2 示例代码
adjacent_find算法查找相邻的重复元素示例代码见下:
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <string>
// 常用查找算法 adjacent_find
// 查找 内置数据类型
void test01()
{
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;
}
else
{
cout << "找到相邻重复元素" << *pos << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
3.3.3 输出
运行上面3.3.2中代码,输出见下:
上面输出为什么返回的是3,见下,在vector容器中相邻重复的元素:
3.4 binary_search二分查找算法
查找指定元素是否存在。
3.4.1 函数原型
函数原型见下:
bool binary_search(iterator beg,iterator end, value);
参数解析:
查找指定的元素,查到 返回true 否则false
注意: 在无序序列中不可用
beg 开始迭代器
end 结束迭代器
value 查找的元素
补:二分查找法查找效率很高,值得注意的是查找的容器中元素必须的有序序列。
3.4.2 示例代码
binary_search二分查找算法示例代码见下:
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <string>
// 常用查找算法 binary_search
// 查找 内置数据类型
void test01()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
// 查找容器中是否有9元素
// 注意:容器必须是有序的序列
// 二分查找
bool ret = binary_search(v.begin(),v.end(),9);
if (ret)
{
cout << "找到了" << endl;
}
else
{
cout << "未找到" << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
3.4.3 输出
运行上面3.4.2中代码,输出见下:
3.5 count统计算法
统计容器中元素的个数。
3.5.1 函数原型
函数原型见下:
count(iterator beg,iterator end,value):
参数解析:
统计元素出现次数
beg 开始迭代器
end 结束迭代器
value 统计的元素
3.5.2 示例代码
count统计容器中元素个数的示例代码见下,有两种情况,统计内置数据类型和统计自定义数据类。
注意:统计自定义数据类型时,需要配合重载operator==重新写代码。
代码见下:
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <string>
// 常用查找算法find_if
// 查找 内置数据类型
void test01()
{
cout << "下面输出是内置数据类型" << endl;
vector<int> v;
v.push_back(10);
v.push_back(40);
v.push_back(30);
v.push_back(40);
v.push_back(20);
v.push_back(40);
// 统计出v容器中有多少个40元素
int num = count(v.begin(),v.end(),40);
cout << "40的元素个数为:" << num << endl;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Person
{
public:
Person(string name,int age)
{
this->m_Name = name;
this->m_Age = age;
}
bool operator == (const Person & p)
{
if (this->m_Age == p.m_Age)
{
return true;
}
else
{
return false;
}
}
string m_Name;
int m_Age;
};
// 查找 自定义数据类型
void test02()
{
cout << "下面是自定义数据类型" << endl;
vector<Person>v;
// 创建数据
Person p1("李元芳",20);
Person p2("狄仁杰",20);
Person p3("鲁班",30);
Person p4("公孙离",20);
Person p5("虞姬",50);
// 将人员数据存放到vector容器中
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
Person p("莱西奥",20);
int num = count(v.begin(),v.end(),p);
cout << "和莱西奥同岁的人员个数为:" << num << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
3.5.3 输出
运行上面3.5.2中代码见下:
3.6 count_if条件统计算法
按照条件统计元素个数。
3.6.1 函数原型
函数原型见下:
count_if(iterator beg,iterator end,_Pred);
参数解析:
按条件统计元素出现次数
beg 开始迭代器
end 结束迭代器
_Pred 谓词
3.6.2 示例代码
count_if条件统计算法示例代码中,有两种情况:内置数据类型和自定义数据类型。两种情况都要单独写仿函数,在仿函数中声明好判断条件,代码见下:
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <string>
// 常用查找算法find_if
// 仿函数
class Greator20
{
public:
bool operator()(int val)
{
return val > 20;
}
};
// 查找 内置数据类型
void test01()
{
cout << "下面输出是内置数据类型" << endl;
vector<int> v;
v.push_back(10);
v.push_back(40);
v.push_back(30);
v.push_back(40);
v.push_back(20);
v.push_back(40);
// 统计成员大于20的个数
int num = count_if(v.begin(),v.end(),Greator20());
cout << "大于20的成员个数为:" << num << endl;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Person
{
public:
Person(string name,int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
// 仿函数
class AgeCreater20
{
public:
bool operator()(const Person & p)
{
return p.m_Age > 20;
}
};
// 查找 自定义数据类型
void test02()
{
cout << endl << "下面是自定义数据类型" << endl;
vector<Person>v;
// 创建数据
Person p1("李元芳",20);
Person p2("狄仁杰",20);
Person p3("鲁班",30);
Person p4("公孙离",20);
Person p5("虞姬",50);
// 将人员数据存放到vector容器中
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
// 统计 大于20岁人员个数
int num = count_if(v.begin(),v.end(),AgeCreater20());
cout << "大于20岁人员个数为:" << num << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
3.6.3 输出
运行上面3.6.2中代码,输出见下:
四、总结
以上就是C++STL中常用的算法:容器遍历和查找的各个接口,希望能帮你理解使用。本人参考学习的是黑马程序员,仅作为笔记记录。
感谢您阅读到最后!😊总结不易,多多支持呀🌹 点赞👍收藏⭐评论✍️,您的三连是我持续更新的动力💖
关注下面「视觉研坊」,获取干货教程、实战案例、技术解答、行业资讯!