【C++】 STL – 算法(一)
前言
一、函数对象
本质是一个类的对象,因此称为函数对象,也叫仿函数
class MyPrint
{
public:
void operator()(int num)
{
cout << num << endl;
m_Count++;
}
int m_Count = 0;
};
void myPrint02(int num)
{
cout << num << endl;
}
void test01()
{
MyPrint myPrint;
myPrint(100); //本质是一个类的对象,因此称为函数对象,也叫仿函数
myPrint02(100);
}
函数对象 超出了普通函数的概念,可以拥有自己状态
void test02()
{
//函数对象 超出了普通函数的概念,可以拥有自己状态
MyPrint myPrint;
myPrint(100);
myPrint(100);
myPrint(100);
myPrint(100);
cout << "调用次数为: " << myPrint.m_Count << endl;
}
函数对象可以作为函数参数
void doPrint(MyPrint myPrint , int num)
{
myPrint(num);
}
void test03()
{
doPrint(MyPrint(), 1000);
}
二、谓词
一元谓词
//一元谓词
class GreaterThan20
{
public:
bool operator()(int val)
{
return val > 20;
}
};
void test01()
{
vector<int>v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);
vector<int>::iterator ret = find_if(v.begin(), v.end(), GreaterThan20());
if ( ret != v.end())
{
cout << "找到大于20的数字为: " << *ret << endl;
}
else
{
cout << "未找到" << endl;
}
}
二元谓词
//二元谓词
void myPrintInt( int val)
{
cout << val << " ";
}
class MyCompare
{
public:
bool operator()(int v1,int v2)
{
return v1 > v2;
}
};
void test02()
{
vector<int>v;
v.push_back(10);
v.push_back(30);
v.push_back(20);
v.push_back(40);
v.push_back(50);
sort(v.begin(), v.end()); //从小到大
for_each(v.begin(), v.end(), myPrintInt);
cout << endl;
sort(v.begin(), v.end(), MyCompare());
//lambda表达式 匿名函数 []代表lambda表达式标志 [](){}
for_each(v.begin(), v.end(), [](int val){ cout << val << " "; });
cout << endl;
}
三、内建函数对象
引入头文件 #include< functional>
取反 negate
加法 plus
大于 greater
/*
template<class T> T negate<T>//取反仿函数 一元运算
*/
void test01()
{
negate<int>n;
cout << n(10) << endl;
}
//template<class T> T plus<T>//加法仿函数
void test02()
{
plus<int> p;
cout << p(10, 10) << endl;
}
//template<class T> bool greater<T>//大于
void test03()
{
vector<int>v;
v.push_back(20);
v.push_back(50);
v.push_back(10);
v.push_back(30);
v.push_back(40);
//从大到小排序
sort(v.begin(), v.end(), greater<int>());
for_each(v.begin(), v.end(), [](int val){cout << val << " "; });
cout << endl;
}
四、适配器
class MyPrint :public binary_function<int,int,void>
{
public:
void operator()(int val , int start)const
{
cout << "val = " << val << " start = " << start << " sum = " <<val + start << endl;
}
};
//1、函数对象适配器
void test01()
{
vector<int>v;
for (int i = 0; i < 10;i++)
{
v.push_back(i);
}
cout << "请输入起始累加值: " << endl;
int num;
cin >> num;
for_each(v.begin(), v.end(), bind2nd( MyPrint(), num ) );
//for_each(v.begin(), v.end(), bind1st(MyPrint(), num));
}
//1、利用bind2nd 进行绑定
//2、继承 public binary_function<参数1 类型,参数2类型,返回值类型>
//3、加const
取反适配器
//2、取反适配器
class GreaterThanFive:public unary_function<int,bool>
{
public:
bool operator()(int val) const
{
return val > 5;
}
};
void test02()
{
vector<int>v;
for (int i = 0; i < 10;i++)
{
v.push_back(i);
}
//一元取反
//vector<int>::iterator pos = find_if(v.begin(), v.end(), not1( GreaterThanFive()));
vector<int>::iterator pos = find_if(v.begin(), v.end(), not1( bind2nd( greater<int>() , 5 )));
if (pos != v.end())
{
cout << "找到小于5的值为: " << *pos << endl;
}
else
{
cout << "未找到" << endl;
}
//二元取反
sort(v.begin(), v.end(), not2 (less<int>()));
for_each(v.begin(), v.end(), [](int val){cout << val << endl; });
}
//1、利用not1进行取反
//2、继承 public unary_function<int,bool>
//3、加const
函数适配器
void myPrint3( int val , int start)
{
cout << val + start << endl;
}
//3、 函数适配器
void test03()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//将函数指针 适配成函数对象 ptr_fun
for_each(v.begin(), v.end(), bind2nd(ptr_fun(myPrint3), 1000));
}
成员函数适配器
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
void showPerson()
{
cout << "成员函数----姓名: " << this->m_Name << " 年龄: " << this->m_Age << endl;
}
void addAge()
{
this->m_Age += 100;
}
string m_Name;
int m_Age;
};
//void myPrint4( Person & p)
//{
// cout << "姓名: " << p.m_Name << " 年龄: " << p.m_Age << endl;
//}
void test04()
{
vector< Person > v;
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
//利用 mem_fun_ref
for_each(v.begin(), v.end(), mem_fun_ref(&Person::showPerson));
for_each(v.begin(), v.end(), mem_fun_ref(&Person::addAge));
for_each(v.begin(), v.end(), mem_fun_ref(&Person::showPerson));
}
总结
1函数对象
1.1本质是一个类的对象,因此称为函数对象,也叫仿函数
1.2函数对象 超出了普通函数的概念,可以拥有自己状态
1.3函数对象可以作为函数参数
2谓词
2.1普通函数或者仿函数的返回值是bool类型,称为谓词
2.2一元谓词
2.2.1查找容器中大于20的数字 find_if
2.3二元谓词
2.3.1对容器进行排序 sort
2.4lambda表达式 {}
3内建函数对象
3.1引入头文件 #include< functional>
3.2取反 negate
3.3加法 plus
3.4大于 greater
4适配器
4.1函数对象适配器
4.1.1//1、利用bind2nd 进行绑定
4.1.2//2、继承 public binary_function<参数1 类型,参数2类型,返回值类型>
4.1.3//3、加const
4.2取反适配器
4.2.1一元取反 not1
4.2.1.1//1、利用not1进行取反
4.2.1.2//2、继承 public unary_function<int,bool>
4.2.1.3//3、加const
4.2.2二元取反 not2
4.3函数指针适配器
4.3.1ptr_fun将普通函数指针 适配成函数对象
4.4成员函数适配器
4.4.1如果存放的是对象实体 mem_fun_ref
4.4.2如果存放的是对象指针 mem_fun
到这里这篇文章的内容就结束了,谢谢大家的观看,如果有好的建议可以留言喔,谢谢大家啦!