概述:
- 算法主要是由头文件
#include<algorithm>``#include<functional>``#inlcude<numeric>
组成 -
#include<algorithm>
是所有STL头文件中最大的一个,范围涉及到比较、交换、查找、遍历操作、复制,修改等等 -
#include<functional>
体积很小,只包括几个在序列上面进行简单数学运算的模板函数 -
#inlcude<numeric>
定义了一些模板类,用以声明函数对象
算法简介:
-
for_each
遍历容器 -
transform
搬运容器到另一个容器
2.1 for_each
功能描述:
- 实现遍历容器
函数原型:
-
for_each(iterator beg,iterator end,_func);
遍历算法,遍历容器元素
beg开始迭代器
end结束迭代器
_func函数或者函数对象
示例:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用遍历算法 fro_each
//普通函数
void print01(int v1)
{
cout << v1 << " ";
}
//仿函数
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(v.begin(), v.end(), print01);
cout << endl;
for_each(v.begin(), v.end(), MyPrint());//MyPrint()仿函数
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
running
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
2.2 transform
功能描述:
- 搬运容器到另一个容器
函数原型:
-
transform(iterator beg1,iterator end1,iterator beg2,_func);
beg1源容器开始迭代器
end1源容器结束迭代器
beg2目标容器开始迭代器
_func函数或者函数对象
示例:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//transform
class Transform
{
public:
int operator()(int val)
{
return val+10;//也可以做一些逻辑规则
}
};
class MyPrint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void test01()
{
vector<int>v;
for(int i = 0; i < 6; i++)
{
v.push_back(i);
}
vector<int>v2;//目标迭代器
v2.resize(v.size());
transform(v.begin(), v.end(), v2.begin(), Transform());//Transform()仿函数
for_each(v2.begin(), v2.end(), MyPrint());//MyPrint()仿函数
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
running
10 11 12 13 14 15
3.常用的查找算法
算法简介:
-
find
查找元素 -
find_if
按条件查找元素 -
adjacend_find
查找相邻重复元素 -
binary_search
二分查找法 -
count
统计元素个数 -
count_if
按条件统计元素个数
3.1find
功能描述:
- 查找指定元素,找到返回指定容器的迭代器,找不到返回结束迭代器end()
函数原型:
find(iterator beg,iterator end,value);
- 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
- beg 开始迭代器
- end 结束迭代器
- value 查找的元素
示例:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm> //算法的头文件
//常用查找算法-- 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_age = age;
this->m_name = name;
}
//重载“==”让底层知道如何对比数据类型
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()
{
Person p1("孙悟空", 10);
Person p2("猪八戒", 20);
Person p3("沙和尚", 30);
Person p4("唐僧", 40);
Person others("孙悟空", 10);
//存放到容器中
vector<Person>v;
v.push_back(p1);
v.push_back(p1);
v.push_back(p1);
v.push_back(p1);
vector<Person>::iterator it = find(v.begin(), v.end(), others);//查看others是否存在,返回值是一个迭代器
if (it == v.end())
{
cout << "没有找到" << endl;
}
else
{
cout << "找到了" << "姓名:" << it->m_name << " " << "年龄:" << it->m_age << endl;
}
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
running
找到了:5
找到了姓名:孙悟空 年龄:10
find()可以在指定容器中找指定元素,返回值是迭代器
3.2find_if
功能描述:
- 按条件查找元素
函数原型: find_if(iterator beg,iterator end,_Pred);
- 按值查找元素,找到返回指定位置迭代器,到不到返回结束位置迭代器
- _Pred 函数或谓词(返回bool类型的仿函数)
示例:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//find_if
//查找内置数据类型
class GreaterFive
{
public:
bool operator()(int val)
{
return val > 5;//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());//GreaterFive()仿函数
if (it == v.end())
{
cout << "没有找到!" << endl;
}
else
{
cout << "找到了大于五的数据:" << *it << endl;
}
}
//2.查找自定义数据类型
class Person
{
public:
Person(string name, int age)
{
this->m_age = age;
this->m_name = name;
}
string m_name;
int m_age;
};
//谓词
class Greater20
{
public:
bool operator()(Person& p)
{
return p.m_age > 20;
}
};
void test02()
{
vector<Person>v1;
Person p1("孙悟空", 10);
Person p2("猪八戒", 20);
Person p3("沙和尚", 30);
Person p4("唐僧", 40);
v1.push_back(p1);
v1.push_back(p2);
v1.push_back(p3);
v1.push_back(p4);
//找一个年龄大于20的成员
vector<Person>::iterator it = find_if(v1.begin(), v1.end(), Greater20());//Greater20()仿函数
if (it == v1.end())
{
cout << "没有找到" << endl;
}
else
{
cout << "找到了:" << "姓名:" << it->m_name << " 年龄:" << it->m_age << endl;
}
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
running
找到了大于五的数据:6
找到了:姓名:沙和尚 年龄:30
3.3adjacent_find
功能描述:
- 查找相邻重复元素
函数原型:
adjacent_find(iterator beg,iterator end);
- 查找相邻重复元素,返回相邻元素的第一个位置的迭代器
示例:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//adjacent_find()查找相邻重复元素
void test01()
{
vector<int>v;
v.push_back(0);
v.push_back(1);
v.push_back(2);
v.push_back(0);
v.push_back(0);
v.push_back(4);
v.push_back(4);
vector<int>::iterator it = adjacent_find(v.begin(), v.end());
if (it == v.end())
{
cout << "没有找到" << endl;
}
else
{
cout << "找到了:" << *it << endl;
}
}
int main()
{
test01();
//test02();
system("pause");
return 0;
}
running
找到了:0
总结:
- 面试题中如果出现了查找相邻重复元素,记得使用STL中的adjacent_find算法
3.4binary_search
功能描述 :
- 查找指定元素是否存在
函数原型: bool binary_search(iterator beg,iterator end,value);
- 查找指定的元素,找到则返回true,否则返回false
- 注意:无序序列中不可用
示例:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//binary_search()查找指定元素是否存在
//必须在有序的序列中
void test01()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//容器必须是有序序列
bool ret = binary_search(v.begin(), v.end(), 9);
if (ret)
{
cout << "找到了" << endl;
}
else
{
cout << "没有找到" << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
running
找到了
3.5count
功能描述:
- 统计元素个数
函数原型: -
count(iterator beg,iterator end,value);
** 统计元素出现的次数
示例:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//count
//统计内置数据类型
void test01()
{
vector<int>v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(20);
v.push_back(10);
v.push_back(20);
int num = count(v.begin(), v.end(), 20);
cout << "20的个数:" << num << endl;
}
//统计自定义数据类型
class Person
{
public:
Person(string name, int age)
{
this->m_name = name;
this->m_age = age;
}
//重载==运算符
bool operator==(const Person& p)//const必须要加
{
if (this->m_age == p.m_age)
{
return true;
}
else
{
return false;
}
}
string m_name;
int m_age;
};
void test02()
{
vector<Person>v2;
Person p1("孙悟空", 10);
Person p2("猪八戒", 30);
Person p3("沙和尚", 30);
Person p4("红孩儿", 30);
v2.push_back(p1);
v2.push_back(p2);
v2.push_back(p3);
v2.push_back(p4);
Person p("唐僧", 30);
int num2 = count(v2.begin(), v2.end(), p);//统计和唐僧年龄相同的人的个数
cout << "和唐僧年龄相同的人员个数为:" << num2 << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}```
**running**
3.6count_if
功能描述:
- 按条件统计元素个数
函数原型: count_if(iterator bed,iterator end,_Pred);
- 按条件统计元素出现次数
- _Pred谓词
示例:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//count_if
//统计内置数据类型
class Greater20
{
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);
v.push_back(70);
int num = count_if(v.begin(), v.end(), Greater20());//Greater20()仿函数
cout << "大于20的个数:" << 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;
};
class AgeGreater20
{
public:
bool operator()(const Person& p1)
{
return p1.m_age > 20;
}
};
void test02()
{
vector<Person>v2;
Person p1("孙悟空", 10);
Person p2("猪八戒", 30);
Person p3("沙和尚", 30);
Person p4("红孩儿", 30);
v2.push_back(p1);
v2.push_back(p2);
v2.push_back(p3);
v2.push_back(p4);
Person p("唐僧", 30);
int num2 = count_if(v2.begin(), v2.end(),AgeGreater20());
cout << "年龄大于20的人员个数为:" << num2 << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
running
大于20的个数:4
年龄大于20的人员个数为:3
20的个数:3
和唐僧年龄相同的人员个数为:3
4.常用的排序算法
算法简介:
sort
- 对容器内元素进行升序
random_shuffle
- 洗牌 指定范围内的元素随即调整次序
merge
- 容器元素合并,并存储到另一容器中
reverse
- 反转指定范围的元素
4.1sort
功能描述:
- 对容器内元素进行排序
函数原型:
sort(iterator beg,iterator end,_Pred);
- 按值查找元素,找到返回指定元素迭代器,找不到返回结束位置迭代器
- _Pred 谓词
示例:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<functional>
//sort
void MyPrint(int val)
{
cout << val << " ";
}
void test01()
{
vector<int>v;
v.push_back(3);
v.push_back(1);
v.push_back(4);
v.push_back(5);
v.push_back(2);
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;
}
int main()
{
test01();
system("pause");
return 0;
}
running
1 2 3 4 5
5 4 3 2 1