因为c++算法太多,所以将他分成两篇文章去写,这篇文章主要涉及遍历,查找和排序算法
1、遍历算法
(1)for_each
函数原型
for_each(iterator beg, iterator end ,_func);
遍历算法遍历容器元素
beg开始迭代器
end 结束迭代器
func 函数或者函数对象
具体运用如下
#include<iostream>
using namespace std;
#include<vector>
#include<functional>
#include<algorithm>
void print01(int val)
{
cout<<val<<" ";
}
class print02
{
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);
//仿函数
for_each(v.begin(),v.end(),print02());
}
int main()
{
test01();
}
(2)transform
搬运容器到另一个容器中
函数原型
tranform(iterator beg1,iterator end1,iterator beg2,_func)
beg1 源容器开始迭代器
end1 源容器结束迭代器
beg2目标容器开始迭代器
_func函数或者函数对象
具体实例代码如下
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
class print
{
public:
void operator()(int val)
{
cout<<val<<" ";
}
};
class trans
{
public:
int operator()(int val)
{
return val;
}
};
void test01()
{
vector<int>v;
for(int i=0;i<10;i++)
{
v.push_back(i);
}
vector<int>v2;
v2.resize(v.size());
transform(v.begin(),v.end(),v2.begin(),trans());
for_each(v2.begin(),v2.end(),print());
}
int main()
{
test01();
}
2、查找算法
(1)find
查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器end()
函数原型
find(iterator beg,iterator end,value);
按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
beg开始迭代器
end 结束迭代器
value查找的元素
实例代码如下
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//内置数据类型
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(),5);
if(it!=v.end())
{
cout<<"找到了:"<<*it<<endl;
}
else
{
cout<<"未找到"<<endl;
}
}
class Person
{
public:
Person(string name,int age)
{
this->m_Name=name;
this->m_Age=age;
}
bool operator==(const Person &p)
{
if(p.m_Name==this->m_Name&&p.m_Age==this->m_Age)
{
return true;
}
else
{
return false;
}
}
string m_Name;
int m_Age;
};
void test02()
{
vector<Person>v;
Person p1("a",15);
Person p2("b",20);
Person p3("c",56);
Person p4("d",14);
Person p5("e",22);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
Person pp("e",22);
vector<Person>::iterator it=find(v.begin(),v.end(),pp);
if(it!=v.end())
{
cout<<"找到了:"<<"姓名:"<<it->m_Name<<"年龄:"<<it->m_Age<<endl;
}
else
{
cout<<"未找到"<<endl;
}
}
int main()
{
//test01();
test02();
}
(2)find_if
函数原型
find_if(iterator beg, iterator end, _Pred);
按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
beg开始迭代器
end 结束迭代器
Pred函数或者谓词(返回bool类型的仿函数)
使用实例
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//内置数据类型
class Greater
{
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(),Greater());
if(it!=v.end())
{
cout<<"找到了:"<<*it<<endl;
}
else
{
cout<<"未找到"<<endl;
}
}
class Person
{
public:
Person(string name,int age)
{
this->m_Name=name;
this->m_Age=age;
}
string m_Name;
int m_Age;
};
class Greater2
{
public:
bool operator()(Person &p)
{
return p.m_Age>20;
}
};
void test02()
{
vector<Person>v;
Person p1("a",15);
Person p2("b",20);
Person p3("c",56);
Person p4("d",14);
Person p5("e",22);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
vector<Person>::iterator it=find_if(v.begin(),v.end(),Greater2());
if(it!=v.end())
{
cout<<"找到了:"<<"姓名:"<<it->m_Name<<"年龄:"<<it->m_Age<<endl;
}
else
{
cout<<"未找到"<<endl;
}
}
int main()
{
//test01();
test02();
}
(3)adjacent_find
查找相邻重复元素
函数原型
adjacent_find(iterator beg,iterator end);
查找相邻重复元素,返回相邻元素的第一个位置的迭代器
beg 开始迭代器
end 结束迭代器
实例运用
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
void test01()
{
vector<int>v;
v.push_back(1);
v.push_back(2);
v.push_back(5);
v.push_back(2);
v.push_back(4);
v.push_back(4);
v.push_back(3);
vector<int>::iterator it=adjacent_find(v.begin(),v.end());
if(it!=v.end())
{
cout<<"找到了:"<<*it<<endl;
}
else
{
cout<<"未找到"<<endl;
}
}
int main()
{
test01();
}
(4)binary_search
查找指定元素是否存在
函数原型
bool binary_search(iterator beg,iterator end,value);
查找指定的元素,查到返回true 否则false
在无序序列中不可用
beg 开始迭代器
end 结束迭代器
value 查找的元素
实例
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
void test01()
{
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;
}
}
int main()
{
test01();
}
(5)count
统计元素个数
函数原型
count(iterator beg,iterator end,value);
beg 开始迭代器
end 结束迭代器
value统计的元素
实例
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//内置数据类型
/*
void test01()
{
vector<int>v;
v.push_back(2);
v.push_back(3);
v.push_back(2);
v.push_back(3);
v.push_back(2);
int num=count(v.begin(),v.end(),2);
cout<<"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("刘备",35);
Person p2("关羽",40);
Person p3("张飞",35);
Person p4("孙权",35);
Person p5("曹操",34);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
Person pp("诸葛亮",35);
int num=count(v.begin(),v.end(),pp);
cout<<"和诸葛亮同岁的人有"<<num<<"个"<<endl;
}
int main()
{
//test01();
test02();
}
(6)count_if
按条件统计元素个数
函数原型
count_if(iterator beg,iterator end,_Pred);
按条件统计元素出现次数
beg 开始迭代器,
end 结束迭代器
Pred 谓词
实例
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//内置数据类型
class Greater
{
public:
bool operator()(int val)
{
return val>2;
}
};
void test01()
{
vector<int>v;
v.push_back(2);
v.push_back(3);
v.push_back(2);
v.push_back(3);
v.push_back(2);
int num=count_if(v.begin(),v.end(),Greater());
cout<<"大于2的个数为:"<<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 Greater
{
public:
bool operator ()(const Person p)
{
return p.m_Age>35;
}
};
void test02()
{
vector<Person>v;
Person p1("刘备",35);
Person p2("关羽",40);
Person p3("张飞",45);
Person p4("孙权",35);
Person p5("曹操",34);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
int num=count_if(v.begin(),v.end(),Greater());
cout<<"年龄大于35的人有"<<num<<"个"<<endl;
}
*/
int main()
{
test01();
//test02();
}
3、排序算法
(1)sort
对容器内元素进行排序
函数原型
sort(iterator beg,iterator end,_Pred);
按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
beg 开始迭代器,end 结束迭代器
Pred 谓词
实例
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//内置数据类型
/*
class Greater
{
public:
bool operator()(int val1,int val2)
{
return val1>val2;
}
};
void test01()
{
vector<int>v;
v.push_back(2);
v.push_back(3);
v.push_back(1);
v.push_back(5);
v.push_back(4);
sort(v.begin(),v.end());
for(vector<int>::iterator it=v.begin();it!=v.end();it++)
{
cout<<*it<<" ";
}
cout<<endl<<"------"<<endl;
sort(v.begin(),v.end(),Greater());
for(vector<int>::iterator it=v.begin();it!=v.end();it++)
{
cout<<*it<<" ";
}
cout<<endl<<"------"<<endl;
}
*/
//自定义数据类型
class Person
{
public:
Person(string name,int age)
{
this->m_Name=name;
this->m_Age=age;
}
string m_Name;
int m_Age;
};
class Greater01
{
public:
bool operator ()(const Person p1,const Person p2)
{
return p1.m_Age>p2.m_Age;
}
};
class Greater02
{
public:
bool operator ()(const Person p1,const Person p2)
{
return p1.m_Age<p2.m_Age;
}
};
void test02()
{
vector<Person>v;
Person p1("刘备",35);
Person p2("关羽",40);
Person p3("张飞",45);
Person p4("孙权",36);
Person p5("曹操",34);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
sort(v.begin(),v.end(),Greater01());
for(vector<Person>::iterator it=v.begin();it!=v.end();it++)
{
cout<<"姓名:"<<it->m_Name<<" 年龄:"<<it->m_Age<<endl;
}
cout<<endl<<"------"<<endl;
sort(v.begin(),v.end(),Greater02());
for(vector<Person>::iterator it=v.begin();it!=v.end();it++)
{
cout<<"姓名:"<<it->m_Name<<" 年龄:"<<it->m_Age<<endl;
}
}
int main()
{
//test01();
test02();
}
(2)random_shuffle
洗牌,指定范围内的元素随机调整次序
函数原型
random_shuffle(iterator beg,iterator end);
指定范围内的元素随机调整次序
beg开始迭代器
end 结束迭代器
实例
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<ctime>
void print(int val)
{
cout<<val<<" ";
}
void test01()
{
srand((unsigned int)time(NULL));
vector<int>v;
for(int i=0;i<10;i++)
{
v.push_back(i);
}
random_shuffle(v.begin(),v.end());
for_each(v.begin(),v.end(),print);
}
int main()
{
test01();
}
(3)merge
两个容器元素合并,并存储到另一容器中
函数原型
merge(iterator beg1,iterator end1,iterator beg2,iterator end2,iterator dest);
容器元素合并,并存储到另一容器中
注意:两个容器必须是有序的
beg1容器1开始迭代器
end1容器1结束迭代器
beg2容器2开始迭代器
end2容器2结束迭代器
dest 目标容器开始迭代器
实例
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
void print(int val)
{
cout<<val<<" ";
}
void test01()
{
vector<int>v1;
vector<int>v2;
for(int i=0;i<10;i++)
{
v1.push_back(i);
v2.push_back(i+1);
}
vector<int>v3;
v3.resize(v1.size()+v2.size());
merge(v1.begin(),v1.end(),v2.begin(),v2.end(),v3.begin());
for_each(v3.begin(),v3.end(),print);
cout<<endl;
}
int main()
{
test01();
}
(4)reverse
将容器内元素进行反转
函数原型
reverse(iterator beg,iterator end);
反转指定范围的元素
beg开始迭代器,end 结束迭代器
实例
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
void print(int val)
{
cout<<val<<" ";
}
void test01()
{
vector<int>v1;
v1.push_back(5);
v1.push_back(3);
v1.push_back(1);
v1.push_back(4);
v1.push_back(2);
for_each(v1.begin(),v1.end(),print);
reverse(v1.begin(),v1.end());
cout<<endl;
for_each(v1.begin(),v1.end(),print);
cout<<endl;
}
int main()
{
test01();
}
如果这篇文章对你有帮助的话,记得点赞关注哦