函数对象基础
仿函数
#include<iostream> using namespace std; #include<string> class Myclass { public: void operator()(int v1,int v2) { cout << v1 + v2 << endl; } void operator()(string str) { cout << str << endl; } }; void pr(Myclass &m,string str) { m(str); } void test() { Myclass m; string str = "浊心斯卡蒂"; //1.可重载 m(5, 8); m(str); //2.可作为函数对象可作为参数传递 pr(m,str); } int main() { test(); }
运行结果: 13 浊心斯卡蒂 浊心斯卡蒂
谓词
返回bool类型的仿函数成为谓词。
一元谓词
find_if 使用举例
class GreaterFive { public: bool operator()(int val) { return val > 5; } }; void test() { vector<int> v; for (int i = 0; i < 10; i++) v.push_back(i); //find_if返回值是迭代器类型 vector<int>::iterator dddd = find_if(v.begin(), v.end(), GreaterFive()); if (dddd == v.end()) cout << "没找着" << endl; else cout << "是" << *dddd << endl; }
vector<int>::iterator dddd = find_if(v.begin(), v.end(), GreaterFive());
GreaterFive是谓词
二元谓词
sort 使用举例(改变了内定的升序排列)
val1<val2 前面小于后面,升序排列
class compare { public: bool operator()(int val1,int val2) { return val1 < val2; } }; void test() { vector<int> v; for (int i = 0; i < 10; i++) { int x = i % 7; v.push_back(x); } sort(v.begin(), v.end(), compare()); for (vector<int>::iterator beg = v.begin(); beg != v.end(); beg++) cout << *beg << " "; }
运行结果:
0 0 1 1 2 2 3 4 5 6
val1<val2 前面大于后面,将降序排列
class compare { public: bool operator()(int val1,int val2) { return val1 < val2; } }; void test() { vector<int> v; for (int i = 0; i < 10; i++) { int x = i % 7; v.push_back(x); } sort(v.begin(), v.end(), compare()); for (vector<int>::iterator beg = v.begin(); beg != v.end(); beg++) cout << *beg << " "; }
运行结果:
6 5 4 3 2 2 1 1 0 0
算法
STL遍历-for_each
for_each(迭代器起始 ,迭代器终止 ,打印函数);
#include<iostream> using namespace std; #include<string> #include<vector> #include<algorithm> //1普通函数 void print(int val) { cout << val << " "; } //2仿函数 class printff { public: void operator() (int val) { cout << val << " "; } }; void test() { vector<int> v; for (int i = 0; i < 10; i++) { int x = i % 7; v.push_back(x); } for_each(v.begin(), v.end(), print);//函数名即可 cout << endl; for_each(v.begin(), v.end(), printff());//要传一个匿名的函数对象,所以加() } int main() { test(); }
运行结果: 0 1 2 3 4 5 6 0 1 2 0 1 2 3 4 5 6 0 1 2
STL遍历-transform
tranform(原容器迭代器起始,原容器迭代器终止,目标容器迭代器起始,转换函数)
其中转换函数可以在搬运时对数据进行修改,而不能筛选(如去掉下面代码的return 233)
#include<iostream> using namespace std; #include<string> #include<vector> #include<algorithm> class func { public: int operator() (int val) { if (val > 2) return 666; return 233; } }; class func0 { public: void operator() (int val) { cout << val << " "; } }; void test() { vector<int> v; for (int i = 0; i < 10; i++) { int x = i % 7; v.push_back(x); } vector<int> v2; v2.resize(v.size());//要定义目标容器的大小,不然会崩溃。 transform(v.begin(), v.end(), v2.begin(), func());//搬运 for_each(v2.begin(), v2.end(), func0());//遍历 } int main() { test(); }
运行结果: 233 233 233 666 666 666 666 233 233 233
STL查找算法
(1)find查找
find(迭代器起始,迭代器终止,查找数值)
查找第一个符合的元素。
返回迭代器类型。
vector<int>::iterator x=find(v.begin(), v.end(), 2);//寻找元素是否有2 if (x == v.end()) { print("找不到"); } else print("找到");
·查找自定义类型时,先创建一个和查找对象成员属性相同的复制对象,然后查找数值改为复制对象的对象名。
(2)find_if查找
find_if(迭代器起始,迭代器终止,筛选条件仿函数)
查找第一个符合的元素。
返回迭代器类型。
class GreatFive { public: bool operator() (int val) { if (val > 5) return true; return false; } }; vector<int>::iterator x=find_if(v.begin(), v.end(), GreatFive());//寻找元素是否有大于5
(3)adjacent_find查找
查找第一个重复,且相邻的元素。
返回迭代器类型。
vector<int>::iterator x = adjacent_find(v.begin(), v.end());
(4)binary_search查找
容器数值有序才能生效。
查找这个元素是否存在。
返回值布尔类型。
int ret = binary_search(v.begin(), v.end(), 元素);
(5)count查找
统计某个元素出现的次数。
返回值整型。
int num = count(v.begin(), v.end(), 2);//2出现的次数
(6)count_if查找
统计符合条件的元素出现的次数。
返回值整型。
class GreatZero { public: bool operator() (int val) { if (val > 0) return true; return false; } }; int numx = count_if(v.begin(), v.end(), GreatZero()); cout << numx << endl;
排序算法
sort排序
默认升序。
//升序 sort(v.begin(), v.end()); for_each(v.begin(), v.end(), func0()); cout << endl; //降序 sort(v.begin(), v.end(),greater<int>()); for_each(v.begin(), v.end(), func0());
random_shuffle打乱排序
random_shuffle(v.begin(), v.end());
merge合并
两容器内的元素合并。
前提是,两容器的元素有序。
且,合并容器需提前定大小(resize)
vector<int> v1; for (int i = 0; i < 5; i++) v1.push_back(i); vector<int> v2; for (int i = 0; i < 5; i++) v2.push_back(i); vector<int> v3; v3.resize(10);//要提前定大小 merge(v1.begin(), v1.end(), v2.begin(), v2.end(),v3.begin()); for_each(v3.begin(), v3.end(), func0());
reverse反转
容器内元素反转。
reverse(v3.begin(), v3.end());