目录
算法概述
常用遍历算法for_each
常用遍历算法transform
常用查找算法find
常用查找算法find_if
常用查找算法adjacent_find
常用查找算法binary_search
常用查找算法count
常用查找算法count_if
算法概述
算法主要是由头文件<algorithm><functional> <numeric>组成
<algorithm>是所有STL头文件中最大的一个,范围涉及到比较、交换、查找、遍历操作、复制、修改等等。<numeric>体积很小,只包括几个在序列上面进行简单数学运算的模板函数
<functiona>定义了一些模板类用以声明函数对象。
常用遍历算法for_each
for_each //遍历容器
#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include<functional>using namespace std;void print01(int v)
{cout << v << " ";
}
//仿函数
class print02
{
public:void operator()(int i){cout << i<< " ";}
};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;
}
编译运行
常用遍历算法transform
功能描述:
搬运容器到另一个容器中
函数原型:
transform(iterator beg1, iterator end1, iterator beg2, func);
//beg1源容器开始迭代器
//end1源容器结束选代器
//beg2目标容器开始迭代器
//_func函数或者函数对象
#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include<functional>using namespace std;class mytransform
{
public:int operator()(int i){return i + 100;}
};class myprint
{
public:void operator()(int i){cout << i << " ";}
};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(),mytransform());//搬运for_each(v2.begin(),v2.end(),myprint());//输出cout << endl;
}int main()
{test01();return 0;
}
编译运行
常用查找算法find
查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器end()
find//查找元素
find if//按条件查找元素
adjacent find//查找相邻重复元素
binary_search//二分查找法
count//统计元素个数
count if//按条件统计元素个数
#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include<functional>using namespace std;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;} }
public:int m_age;string m_name;
};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 << "没有找到"<< endl;}else{cout << "找到元素:"<<*it << endl;}
}void test02()
{vector<person>v;person p1("aaa",10);person p2("aaa",20);person p3("aaa",30);person p4("aaa",40);v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);vector<person>::iterator it = find(v.begin(),v.end(),p2);if(it == v.end()){cout << "没有找到" << endl;}else{cout << "年龄:" << it->m_age<<"姓名:" <<it->m_name << endl;}
}int main()
{test01();test02();return 0;
}
常用查找算法find_if
find if(iterator beg, iterator end,Pred);// 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
// beg开始选代器
//end 结束选代器
//_Pred函数或者谓词(返回bool类型的仿函数)
#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include<functional>using namespace std;
//内置数据类型
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 << "找到元素:"<<*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;} }
public:int m_age;string m_name;
};class Greater20
{
public:bool operator()(person &p){return p.m_age > 20;}
};void test02()
{vector<person>v;person p1("aaa",10);person p2("aaa",20);person p3("aaa",30);person p4("aaa",40);v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);//找年龄大于20的vector<person>::iterator it = find_if(v.begin(),v.end(),Greater20());if(it == v.end()){cout << "没有找到" << endl;}else{cout << "年龄:" << it->m_age<<"姓名:" <<it->m_name << endl;}
}int main()
{test01();test02();return 0;
}
编译运行
常用查找算法adjacent_find
adjacent find(iterator beg, iterator end);
//查找相邻重复元素,返回相邻元素的第一个位置的选代器
// beg开始选代器
//end结束选代器
#include <set>
#include <map>
#include<functional>using namespace std;void test01()
{vector<int>v;v.push_back(0);v.push_back(1);v.push_back(0);v.push_back(2);v.push_back(2);v.push_back(3);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();return 0;
}
编译运行
找到元素: 6
常用查找算法binary_search
bool binary_search(iterator beg, iterator end, value);
//查找指定的元素,查到返true 否则false
//注意在无序序列中不可用
// beg开始选代器//end结束选代器
// value 查找的元素
#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include<functional>using namespace std;void test01()
{vector<int>v;for(int i = 0;i<10;i++){v.push_back(i);} bool ret = binary_search(v.begin(),v.end(),2);if(!ret){cout << "没有找到"<< endl;}else{cout << "找到元素2"<< endl;}
}int main()
{test01();return 0;
}
常用查找算法count
count(iterator beg, iterator end, value);
// 统计元素出现次数
// beg开始选代器//end结束选代器
// value统计的元素
#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include<functional>using namespace std;
//统计内置数据类型
void test01()
{vector<int>v;for(int i = 0;i<10;i++){v.push_back(i);} int num = count(v.begin(),v.end(),2);cout << "元素2出现的次数:"<< num << 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_age == p.m_age){return true;}else{return false;}}
public:int m_age;string m_name;
};void test02()
{vector<person>v;person p1("aaa",10);person p2("bbb",20);person p3("ccc",30);person p4("ddd",40);person p5("eee",50);person p6("fff",40);v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);v.push_back(p5);int num = count(v.begin(),v.end(),p6);cout << "和fff同龄的有:"<< num<<endl;}
int main()
{test01();test02();return 0;
}
编译运行
常用查找算法count_if
count if(iterator beg, iterator end, Pred):
// 按条件统计元素出现次数
// beg开始迭代器
//end结束选代器
//_Pred谓词
#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include<functional>using namespace std;
//统计内置数据类型
class greater2
{
public:bool operator()(int val){return val > 2;}
};void test01()
{vector<int>v;for(int i = 0;i<10;i++){v.push_back(i);} int num = count_if(v.begin(),v.end(),greater2());cout << "元素大于2出现的次数:"<< num << 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_age == p.m_age){return true;}else{return false;}}
public:int m_age;string m_name;
};class greater20
{
public:bool operator()(const person &p){return p.m_age > 20;}
};void test02()
{vector<person>v;person p1("aaa",10);person p2("bbb",20);person p3("ccc",30);person p4("ddd",40);person p5("eee",50);person p6("fff",40);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(),greater20());cout << "大于20岁的人有:"<< num<<endl;}
int main()
{test01();test02();return 0;
}
编译运行