C++编程:常见内置算法
C++中算法主要是由头文件<algorithm><functional><numeric>组成。
<algorithm>是所有STL头文件中最大的一个算法头文件,范围涉及到比较、 交换、查找、遍历操作、复制、修改等等。
<numeric>体积很小,只包括几个在序列上面进行简单数学运算的模板函数<functiona1>定义了一些模板类,用以声明函数对象。
1 遍历算法
1.1 for_each
函数原型:
- for_each(iterator beg,iterator end,_func);
- // 遍历算法 遍历容器元素
- // beg 开始迭代器
- // end 结束迭代器
- //_func 函数或者函数对象
代码如下:
#include <iostream>
using namespace std;
#include<algorithm>
#include<vector>
//_func普通函数
void print01(int val)
{
cout<<val<<" ";
}
//_func仿函数
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();
return 0 ;
}
输出如下:
1.2 transfom
功能:搬运容器中的内容到另一个容器中
函数原型:
transform(iterator beg1,iterator end1,iterator beg2,_func);
//beg1 源容器开始迭代器
//end1 源容器结束迭代器
//beg2 目标容器开始迭代器
// _func 函数或者函数对象
代码如下:
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
class Transform
{
public:
int operator()(int v)
{
return v;
}
};
class MyPrint
{
public:
void operator()(int val)
{
cout<<val<<' ';
}
};
void test01()
{
vector<int> v1;
for(int i=0;i<10;i++)
{
v1.push_back(i);
}
vector<int> v2;
v2.resize(v1.size());//目标容器需要提前开辟空间
transform(v1.begin(),v1.end(),v2.begin(),Transform());
for_each(v2.begin(),v2.end(),MyPrint());
cout<<endl;
}
int main()
{
test01();
return 0;
}
输出如下:
错误示例:目标容器未提前开辟空间
2 查找算法
2.1 find
功能:查找元素,找到返回指定元素的迭代器,找不到返回结束迭代器end()
函数原型:
find(iterator beg,iterator end, value);
// 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
// beg 开始迭代器
// end 结束迭代器
// value 查找的元素
代码如下:
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
#include<string>
//查找内置数据类型
void test01()
{
vector<int> v;
for(int i=0;i<10;i++)
{
v.push_back(i);
}
vector<int>::iterator it=find(v.begin(),v.end(),4);
if(it==v.end())
{
cout<<"none"<<endl;
}
else
{
cout<<"find: "<<*it<<endl;
}
vector<int>::iterator it2=find(v.begin(),v.end(),44);
if(it2==v.end())
{
cout<<"none"<<endl;
}
else
{
cout<<"find: "<<*it2<<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_Name==p.m_Name && this->m_Age==p.m_Age)
{
return true;
}
else
{
return false;
}
}
string m_Name;
int m_Age;
};
void test02()
{
vector<Person> v;
Person p1("aaa",22);
Person p2("bbb",25);
Person p3("ccc",14);
Person p4("ddd",24);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
Person pp("ddd",24);
vector<Person>::iterator it=find(v.begin(),v.end(),pp);
if(it==v.end())
{
cout<<"none"<<endl;
}
else
{
cout<<"find name: "<<(*it).m_Name<<" age:"<<(*it).m_Age<<endl;
}
}
int main()
{
test01();
test02();
return 0;
}
输出如下:
2.2 find_if
功能:按条件查找元素
函数原型:
find_if(iterator beg, iterator end,_Pred);
// 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
// beg 开始迭代器
// end 结束迭代器
//_Pred 函数或者谓词(返回bool类型的仿函数)
代码如下:
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
#include<string>
//查找内置数据类型
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<<"none"<<endl;
}
else
{
cout<<"find: "<<*it<<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_Name==p.m_Name && this->m_Age==p.m_Age)
{
return true;
}
else
{
return false;
}
}
string m_Name;
int m_Age;
};
class Greater20
{
public:
bool operator()(Person & p)
{
return p.m_Age>20;
}
};
void test02()
{
vector<Person> v;
Person p1("aaa",22);
Person p2("bbb",25);
Person p3("ccc",14);
Person p4("ddd",24);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
vector<Person>::iterator it=find_if(v.begin(),v.end(),Greater20());
if(it==v.end())
{
cout<<"none"<<endl;
}
else
{
cout<<"find name: "<<(*it).m_Name<<" age:"<<(*it).m_Age<<endl;
}
}
int main()
{
test01();
test02();
return 0;
}
输出 如下:
2.3 adjacent_find
功能:查找相邻重复元素
函数原型:
adjacent_find(iterator beg, iterator end);
// 查找相邻重复元素,返回相邻元素的第一个位置的迭代器
// beg 开始迭代器
// end 结束迭代器
代码如下:
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
#include<string>
//查找内置数据类型
void test01()
{
vector<int> v;
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(2);
v.push_back(1);
v.push_back(7);
v.push_back(5);
vector<int>::iterator it=adjacent_find(v.begin(),v.end());
if(it==v.end())
{
cout<<"none"<<endl;
}
else
{
cout<<"find: "<<*it<<endl;
}
vector<int> v2;
v2.push_back(2);
v2.push_back(3);
v2.push_back(4);
v2.push_back(4);
v2.push_back(1);
v2.push_back(7);
v2.push_back(5);
vector<int>::iterator it2=adjacent_find(v2.begin(),v2.end());
if(it2==v2.end())
{
cout<<"none"<<endl;
}
else
{
cout<<"find: "<<*it2<<endl;
}
}
int main()
{
test01();
return 0;
}
输出如下:
2.4 binary_search
功能:二分查找法
函数原型:
bool binary_search(iterator beg,iterator end, value);
// 查找指定的元素,查到返回true 否则false
// 注意: 在无序序列中不可用
// beg 开始迭代器
// end 结束迭代器
// value 查找的元素
代码如下:
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
void test01()
{
vector<int> v;
for(int i=0;i<10;i++)
{
v.push_back(i);
}
bool ret=binary_search(v.begin(),v.end(),4);
if(ret)
{
cout<<"find it!"<<endl;
}
else
{
cout<<"none"<<endl;
}
v.push_back(7);//插入新元素后是一个无序序列,用binary_search查找结果不准确
bool ret2=binary_search(v.begin(),v.end(),9);
if(ret2)
{
cout<<"find it!"<<endl;
}
else
{
cout<<"none"<<endl;
}
}
int main()
{
test01();
return 0;
}
输出如下:
2.5 count
功能:统计元素个数
函数原型:
count(iterator beg,iterator end,value);
// 统计元素出现次数
// beg 开始迭代器
// end 结束迭代器
// value 统计的元素
代码如下:
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
#include<string>
//统计内置数据类型
void test01()
{
vector<int> v;
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(2);
v.push_back(1);
v.push_back(7);
v.push_back(5);
int num=count(v.begin(),v.end(),2);
cout<<"number of 2: "<<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()
{
vector<Person> v;
Person p1("aaa",23);
Person p2("bbb",34);
Person p3("ccc",52);
Person p4("ddd",14);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
Person p("addf",23);
int num=count(v.begin(),v.end(),p);
cout<<"number of same age as p: "<<num<<endl;
}
int main()
{
test01();
test02();
return 0;
}
输出如下:
2.6 count_if
功能:按条件统计元素个数
函数原型:
count_if(iteratdr beg, iterator end,_Pred);// 按条件统计元素出现次数
// beg 开始迭代器
// end 结束迭代器
// _Pred 谓词
代码如下:
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
#include<string>
//统计内置数据类型
class GreaterFive
{
public:
bool operator()(int val)
{
return val>5;
}
};
void test01()
{
vector<int> v;
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(2);
v.push_back(1);
v.push_back(7);
v.push_back(5);
int num=count_if(v.begin(),v.end(),GreaterFive());
cout<<"number of >5: "<<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 Greater15
{
public:
bool operator()(Person &p)
{
return p.m_Age>15;
}
};
void test02()
{
vector<Person> v;
Person p1("aaa",23);
Person p2("bbb",34);
Person p3("ccc",52);
Person p4("ddd",14);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
Person p("addf",23);
int num=count_if(v.begin(),v.end(),Greater15());
cout<<"number of age>15: "<<num<<endl;
}
int main()
{
test01();
test02();
return 0;
}
输出如下:
3 排序算法
3.1 sort
3.2 random_shuffle
3.3 merge
3.4 reverse
4 拷贝和替换算法
4.1 copy
4.2 replace
4.3 replace_if
4.4 swap
5 算术生成算法
5.1 accumulate
5.2 fill
6 集合算法
6.1 set_intersection
6.2 set_union
6.3 set_difference