1,stl函数对象的分类
a,算数仿函数
b,关系仿函数
c,逻辑仿函数
这些函数与一般函数相同,使用内建函数对象,需要引入头文件#include
2,算术仿函数
这里举两个例子plus和negate
#include<functional>
#include<iostream>
using namespace std;
int main()
{
negate<int> p;//取反
cout << p(50) << endl;
plus<int> c;//加法
cout << c(10, 50) << endl;
return 0;
}
3,关系仿函数
实现关系对比
greater()
#include<functional>
#include<iostream>
using namespace std;
#include<vector>
#include<./algorithm>
int main()
{
vector<int> sh;
sh.push_back(50);
sh.push_back(40);
sh.push_back(30);
sh.push_back(80);
sh.push_back(1000);
sh.push_back(304);
sh.push_back(500);
sh.push_back(440);
sh.push_back(303);
sh.push_back(504);
sh.push_back(407);
sh.push_back(3090);
sort(sh.begin(), sh.end(), greater<int>());//匿名函数对象
vector<int>::iterator it = sh.begin();
for (;it!=sh.end();it++)
{
cout << *it << endl;
}
return 0;
}
4,逻辑仿函数基本用不到。
5常用算法函数
for_each()
#include<functional>
#include<iostream>
using namespace std;
#include<vector>
#include<./algorithm>
class print
{
public:
void operator()(int val)
{
cout << val << endl;
}
};
void myprint(int sh)
{
cout << sh<< endl;
}
int main()
{
vector<int> sh;
sh.push_back(50);
sh.push_back(40);
sh.push_back(30);
sh.push_back(80);
sh.push_back(1000);
sh.push_back(304);
sh.push_back(500);
sh.push_back(440);
sh.push_back(303);
sh.push_back(504);
sh.push_back(407);
sh.push_back(3090);
sort(sh.begin(), sh.end(), greater<int>());
for_each(sh.begin(),sh.end(),myprint);//函数
for_each(sh.begin(),sh.end(),print());//匿名函数对象
vector<int>::iterator it = sh.begin();
for (;it!=sh.end();it++)
{
cout << *it << endl;
}
return 0;
}
transform交换两个模板
#include<iostream>
#include<./algorithm>
#include<./functional>
#include<vector>
using namespace std;
class Trans
{
public:
int operator()(int val)
{
return val+10;
}
};
void myprint(int val)
{
cout<<val<<endl;
}
int main()
{
vector<int> p;
vector<int>target;
for (int i=0;i<10;i++)
{
p.push_back(i);
}
target.resize(p.size());
for_each(p.begin(),p.end(),myprint);
transform(p.begin(),p.end(),target.begin(),Trans());
for_each(target.begin(), target.end(), myprint);
return 0;
}
常用查找算法
#include<iostream>
#include<./algorithm>
#include<./functional>
#include<vector>
using namespace std;
#include<string>
//常用查找算法
//find,find_if,
class person
{
public:
person(int age,string name)
{
this->m_age = age;
this->m_name = name;
}
bool operator==(const person &a)//重载==
{
if (this->m_age == a.m_age && this->m_name == a.m_name)
{
return true;
}
else
{
return false;
}
}
public:
int m_age;
string m_name;
};
int main()
{
person a(20,"name");
person b(20, "h");
person c(20, "ng");
person d(20, "me");
person e(100, "ame");
person f(50, "namfe");
person g(60, "namfae");
vector<person> vec;
vec.push_back(a);
vec.push_back(b);
vec.push_back(c);
vec.push_back(d);
vec.push_back(e);
vec.push_back(f);
vec.push_back(g);
person pp(20, "h");
vector<person>::iterator it = find(vec.begin(),vec.end(),pp);//查找指定容器数据
cout<<it->m_age<<" "<<it->m_name << endl;
return 0;
}
find_if
#include
#include<./algorithm>
#include<./functional>
#include
using namespace std;
#include
//常用查找算法
//find,find_if,
class person
{
public:
person(int age,string name)
{
this->m_age = age;
this->m_name = name;
}
int operator()(const person &a)
{
return a.m_age > 20;
}
public:
int m_age;
string m_name;
};
class great
{
public:
bool operator()(person& a)
{
return a.m_age > 20;
}
};
int main()
{
person a(20,"name");
person b(20, "h");
person c(20, "ng");
person d(20, "me");
person e(100, "ame");
person f(50, "namfe");
person g(60, "namfae");
vector<person> vec;
vec.push_back(a);
vec.push_back(b);
vec.push_back(c);
vec.push_back(d);
vec.push_back(e);
vec.push_back(f);
vec.push_back(g);
vector<person>::iterator it = find_if(vec.begin(),vec.end(),great());//查找指定容器数据(条件成立)
cout<<it->m_age<<" "<<it->m_name << endl;
it++;
cout << it->m_age << " " << it->m_name << endl;
it++;
cout << it->m_age << " " << it->m_name << endl;
return 0;
}
adjacent_find()作用查找stl模板中是否有相同的元素
binary_find()作用查找stl模板中是否有此元素
count作用是统计次数
count_if按条件统计次数
常用排序算法
sort(const _RanIt _First, const _RanIt _Last, _Pr _Pred)默认为从小到大
rrandom_shuffle(_RanIt _First, _RanIt _Last)打乱次序
void reverse(const _BidIt _First, const _BidIt _Last)翻转次序
merge(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _InIt2 _Last2, _OutIt _Dest, _Pr _Pred)合并两个容器
常用拷贝算法
copy(_InIt _First, _InIt _Last, _OutIt _Dest)//复制一个容器到另外一个容器
replace(const _FwdIt _First, const _FwdIt _Last, const _Ty & _Oldval, const _Ty & _Newval)//改变原来的旧值
replace_if(const _FwdIt _First, const _FwdIt _Last, _Pr _Pred, const _Ty & _Val)//按条件改变
swap(container c1,container c2)//交换两个容器
常用算术生成算法
accumulate()//计算容器中的总和
fill()//向容器中添加元素
常用集合算法
set_intersectoin()求两个容器里面的交集结果存储在第三个容器中
set_union()求两个容器并集结果存储在第三个容器中,必须有序
set_difference()求两个容器差集结果存储在第三个容器中,必须有序