C++ STL学习笔记(二)

琐记:
 上一篇文章是:C++ STL学习(一),想要了解容器的接口以及各个容器有什么异同都在上篇文章里面。
 这一篇文章用来记录函数对象

4.1 STL- 函数对象

一、本质 概念 特点

本质

 函数对象(仿函数)是一个类,不是一个函数

概念

 重载函数调用操作符的类,其对象常称为函数对象
 函数对象使用重载的()时,行为类型函数调用,也叫仿函数

特点

 函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
 函数对象超出普通函数的概念,函数对象可以有自己的状态
  函数对象可以作为参数传递(前面涉及容器更改排序规则用到过)

二、函数对象使用

#include <iostream>
#include<string>

using namespace std;

//1、函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
class MyAdd
{
    
    
public:
   int operator()(int v1, int v2)
   {
    
    
   	return v1 + v2;
   }
};

void test01()
{
    
    
   MyAdd myadd;
   cout << myadd(10, 10) << endl;
}

//2、函数对象超出普通函数的概念,函数对象可以有自己的状态
class MyPrint{
    
    
public:
   MyPrint(){
    
    
   	this->count = 0;
   }
   void operator()(string test){
    
    
   	cout << test << endl;
   	this->count++;
   }

   int count;	//内部自己的状态
};

void test02(){
    
    
   MyPrint myPrint;
   myPrint("hello world");
   myPrint("hello world");
   myPrint("hello world");
   cout << "myPrint调用次数为:" << myPrint.count << endl;
}

//3、函数对象可以作为参数传递
void doPrint(MyPrint& myPrint, string test){
    
    
   myPrint(test);
}

void test03(){
    
    
   MyPrint myPrint;
   doPrint(myPrint, "hello c++");
}


int main(){
    
    
   test01();
   test02();
   test03();
}

4.2 谓词

概念

返回bool类型的仿函数称为谓词
如果operator()接受1个参数,那么叫一元谓词
如果operator()接受2个参数,那么叫二元谓词

谓词的使用

一元谓词

#include<iostream>
#include<vector>
#include<algorithm>
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);
	}
	
	//查找容器中有没有大于5的数字
	//GreaterFive() 匿名函数对象
	vector<int>::iterator it = find_if(v.begin(),v.end(),GreaterFive());
	if(it == v.end())
		cout<<"no number greater than five"<< endl;
	else
		cout<<*it<<endl;
}
int main(){
    
    
	test01();
	return 0;
} 

二元谓词使用

#include <iostream>
#include<vector>
#include<algorithm>
 
using namespace std;
 
//2、二元谓词
class MyCompare
{
    
    
public:
	bool operator()(int v1, int v2){
    
    
		return v1 > v2;
	}
};
 
void test01(){
    
    
	vector<int> v;
	v.push_back(10);
	v.push_back(40);
	v.push_back(20);
	v.push_back(30);
	v.push_back(50);
	
	sort(v.begin(), v.end());
 
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++){
    
    
		cout << *it << " ";
	}
	cout << endl;
 
	//使用函数对象,改变算法策略,变为排序规则从大到小
	sort(v.begin(), v.end(), MyCompare());
 
	cout << "排序后" << endl;
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++){
    
    
		cout << *it << " ";
	}
	cout << endl;

}

int main(){
    
    
	test01();
}

4.3 内建函数对象

概念 分类 用法

概念:

STL内建了一些函数对象

分类:

算术仿函数
关系仿函数
逻辑仿函数

用法:

这些仿函数所产生的对象,用法和一般函数完全相同
使用内建函数对象 需头文件#include <functional>

①算数仿函数

功能描述:

实现加减乘除取模等运算

注意:

其中negate是一元运算,其他都是二元运算

仿函数原型:

template<class T> T plus<T>			//加法
template<class T> T minus<T>		//减法
template<class T> T multiplies<T>	//乘法
template<class T> T divides<T>		//除法
template<class T> T modulus<T>		//取模
template<class T> T negate<T>		//取反

例:

//一元运算 取反
negate<int>n;
cout << n(10) << endl;
//二元运算 加法 默认传的都是同类型
plus<int>m;
cout << m(10, 20) << endl;

②关系仿函数

功能描述:

 实现关系对比;
 因为在sort底层实现的时候默认就是 less<>,所以都是升序排列;我们需要常用greater<>
 常用匿名对象使用

仿函数原型:

template<class T> bool equal_to<T>		//等于
template<class T> bool not_equal_to<T>	//不等于
template<class T> bool greater<T>		//大于
template<class T> bool greater_equal<T>	//大于等于
template<class T> bool less<T>			//小于
template<class T> bool less_equal<T>	//小于等于

例:

//利用匿名对象 实现从大到小排列
sort(v.begin(),v.end(),greater<int>());

③逻辑仿函数

功能描述:

 实现关系对比;

仿函数原型:

template<class T> bool logical_and<T> //与
template<class T> bool logical_or<T>  //或
template<class T> bool logical_not<T> //非

例:

//利用匿名对象 实现取反
vector<bool>v;
v.push_back(true);
v.push_back(false);
v.push_back(true);
v.push_back(false);

vector<bool>v2;
v2.resize(v.size());

transform(v.begin(), v.end(), v2.begin(), logical_not<bool>());

猜你喜欢

转载自blog.csdn.net/Stanford_sun/article/details/122777188