STL学习(?常用的遍历算法和查找算法)
目录
一、常用遍历算法for_each
1. 使用函数
2.使用函数对象
二、常用遍历算法transform
三、常用查找算法find
1. 查找内置数据类型
2.查找自定义数据类型
四、常用查找算法find_if
1. 查找内置数据类型
2. 查找自定义数据类型
五、常用查找算法adjacent_find
六、常用查找算法binary_search
七、常用查找算法count
1.内置数据类型
2.自定义数据类型
八、常用查找算法count_if
1. 内置数据类型
2. 自定义数据类型
一、常用遍历算法for_each
函数原型
for_each(iterator beg, iterator end, _func)
// beg 开始迭代器
// end 结束迭代器
// _func 函数或者函数对象
1. 使用函数
2.使用函数对象
二、常用遍历算法transform
函数原型
transform(iterator beg1, iterator end1, iterator beg2, _func)
// beg1 源容器开始跌迭代器
// end1 源容器结束迭代器
// beg2 目标容器开始迭代器
// _func 函数或者函数对象
三、常用查找算法find
函数原型
find(iterator beg, iterator end, int value) // beg 开始迭代器,end结束迭代器, value查找的值
找到返回指定位置迭代器,找不到返回end。
1. 查找内置数据类型
2.查找自定义数据类型
#include<iostream> #include<algorithm> #include<vector> #include<string> using namespace std;class Person { public:string name;int age;Person(string name,int age){this->name = name;this->age = age;}bool operator==(const Person &p){if(this->name == p.name && this->age == p.age){return true;}return false;} };void test2() { vector<Person> v;v.push_back(Person("张三",18));v.push_back(Person("李四",19));v.push_back(Person("王五",20));vector<Person>::iterator it = find(v.begin(),v.end(), Person("张三",18));if(it!=v.end()){cout<< "找到" << endl;}else{cout<< "未找到" << endl;} }int main() {test2();return 0; }
四、常用查找算法find_if
函数原型
find_if(iterator beg, iterator end, _Pred) // beg 开始迭代器, end 结束迭代器, _Pred 谓词。 返回第一个满足条件的迭代器。
1. 查找内置数据类型
2. 查找自定义数据类型
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;class Person
{public:int age;string name;Person(int a,string n){age=a;name=n;}
};
class Myfind
{public:bool operator()(Person& p){return p.age>3;}
};void test2()
{ vector<Person> v;v.push_back(Person(1,"a"));v.push_back(Person(2,"b"));v.push_back(Person(3,"c"));v.push_back(Person(4,"d"));v.push_back(Person(5,"e"));vector<Person>::iterator it=find_if(v.begin(),v.end(),Myfind());if(it!=v.end()){cout<<"find it"<<endl;}
}int main()
{test2();return 0;
}
五、常用查找算法adjacent_find
函数原型
adjacent_find(iterator beg, iterator end) // 查找相邻重复元素,找到了就返回第一个重复元素的迭代器,找不到就返回end
六、常用查找算法binary_search
函数原型
bool binary_search(iterator beg, iterator end, value) // beg 开始迭代器, end 终止迭代器, value查找的值
###### 注意在无序的序列中不可用!!!
七、常用查找算法count
函数原型
count(iterator beg, iterator end , value) // beg 开始迭代器,end 结束迭代器, value 要计数的值
1.内置数据类型
2.自定义数据类型
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;class Person
{public:string name;int age;Person(string name,int age){this->name=name;this->age=age;}bool operator==(const Person &p){if(this->name==p.name&&this->age==p.age){return true;}return false;}
};void test2()
{ vector<Person> v;v.push_back(Person("张三",18));v.push_back(Person("李四",19));v.push_back(Person("王五",20));v.push_back(Person("赵六",20));int num=count(v.begin(),v.end(),Person("王五",20));if(num>0){cout<< num << endl;}
}int main()
{test2();return 0;
}
八、常用查找算法count_if
函数原型
count_if(iterator begin, iterator end, _Pred) // beg 开始迭代器, end 结束迭代器, _Pred 谓词
1. 内置数据类型
2. 自定义数据类型
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
class Person
{public:int age;string name;Person(int a,string n){age=a;name=n;}
};
class Myfind
{public:bool operator()(Person& p){return p.age>3;}
};void test2()
{ vector<Person> v;v.push_back(Person(1,"a"));v.push_back(Person(2,"b"));v.push_back(Person(3,"c"));v.push_back(Person(4,"d"));v.push_back(Person(5,"e"));int num=count_if(v.begin(),v.end(),Myfind());if(num>0){cout<<"find it:" << num << endl;}
}int main()
{test2();return 0;
}