1. 程式人生 > >函式物件,一元謂詞,二元謂詞,預定義函式物件

函式物件,一元謂詞,二元謂詞,預定義函式物件

find_if  ,  sort ,  for_each 的使用 for_each(v1.begin(), v1.end(), ShowElemt<int>() ) ;  要使用預定義函式物件需要包含 functional 標頭檔案
vector<int>::iterator it = find_if(v1.begin(), v1.end(), myint )  sort(v3.begin(),v3.end(),Compare) ; // 按從大到小(遞減)排序 
#include <iostream> 
#include <vector>
#include <algorithm>
using namespace std ; 
//定義函式物件 (過載函式運算子的類)
template <typename T>
class ShowElem {
	public:
		ShowElem(){
			n = 0 ;
		} 
		void operator()(T &t)
		{
			cout << t << " ";
			++ n ;
		}
		void printN(){
			cout <<"n " << n << " " << endl;
		}
	protected:
	private:
		int n ;
};

void Show(int & a)
{
	cout << a << " "  ;
}
int main(){
	ShowElem<int> showElemt ;
	vector<int> v1 ;
	v1.push_back(1);
	v1.push_back(2);
	v1.push_back(3);
showElemt = for_each(v1.begin(), v1.end(), showElemt ) ;
//showElemt = for_each(v1.begin(), v1.end(), ShowElemt<int>() ) ;
// 第三個引數為匿名函式物件,匿名仿函式  此處即可遍歷輸出。
// 函式物件能記錄部分狀態資訊。  返回一個函式物件即可,再呼叫相應的函式輸出狀態。 
showElemt.printN();
for_each(v1.begin(), v1.end(),Show ) ;
} 
//ShowElem<int> showElemt ;
//showElemt(a)  函式物件的() 執行像一個函式//仿函式 

一元謂詞:函式引數一個,函式返回值是bool型別 ; 二元謂詞:函式引數二個,函式返回值是bool型別

find_if  返回的是一個迭代器。使用案例:

#include <iostream> 
#include <vector>
#include <algorithm>
using namespace std ; 
template <class T >
class Lv{
	public :
		Lv(const T & dis)
		{
			this->dis = dis ;
		}
		bool operator()(T &t){
			return (t%dis == 0) ;
		}
	protected :
	private :
		T dis ;
};
int main(){
	vector<int> v1 ;
	for( int i=1; i<10; ++i)
	{
		v1.push_back(i);
	}
	int a = 3 ;
	Lv<int> myint(a) ;
	vector<int>::iterator it = find_if(v1.begin(), v1.end(), myint ) ; // 返回的是迭代器 
	cout << *it ;
} // 程式輸出的是 vector中第一個能被3整除的數。

二元謂詞使用場景:可對兩個物件進行操作,(+ - * / 或者是大小比較)

下面是二元函式物件的案例

#include <iostream> 
#include <vector>
#include <algorithm>
using namespace std ; 
//定義函式物件 

template <typename T>
class ShowElem {
	public:
		ShowElem(){
			n = 0 ;
		} 
		void operator()(T &t)
		{
			cout << t << " ";
			++ n ;
		}
		void printN(){
			cout <<"n " << n << " " << endl;
		}
	protected:
	private:
		int n ;
};

template <class T >
class Sumadd{
	public :
		T operator()(T t1,T t2){
			return t1+t2 ;
		}
	protected :
	private :
		T dis ;
};

int main(){
	vector<int> v1 ;
	vector<int> v2 ;
	vector<int> v3 ;
	for( int i=1; i<=10; ++i)
	{
		v1.push_back(i);
		v2.push_back(i);
	}
	int a = 3 ;
	v3.resize(10) ;
transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), Sumadd<int>( )) ; // 返回的是迭代器 
for_each(v3.begin(),v3.end(),ShowElem<int>()) ;
}
二元謂詞的案例:
#include <iostream> 
#include <vector>
#include <algorithm>
using namespace std ; 
//定義函式物件 

template <typename T>
class ShowElem {
	public:
		ShowElem(){
			n = 0 ;
		} 
		void operator()(T &t)
		{
			cout << t << " ";
			++ n ;
		}
		void printN(){
			cout <<"n " << n << " " << endl;
		}
	protected:
	private:
		int n ;
};

template <class T >
class Sumadd{
	public :
		T operator()(T t1,T t2){
			return t1+t2 ;
		}
	protected :
	private :
		T dis ;
};

bool Compare(const int &a , const int &b)
{
	return a > b ;
}
int main(){
	vector<int> v1 ;
	vector<int> v2 ;
	vector<int> v3 ;
	for( int i=1; i<=10; ++i)
	{
		v1.push_back(i);
		v2.push_back(i);
	}
	int a = 3 ;
	v3.resize(10) ;
transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), Sumadd<int>( )) ; // 返回的是迭代器 
sort(v3.begin(),v3.end(),Compare) ; // 按從大到小(遞減)排序 
for_each(v3.begin(),v3.end(),ShowElem<int>()) ;
}

set.find 函式預設會區分大小寫,要不區分大小寫,可自己定義一個仿函式(函式物件) 然後在定義set時

set<int , ComapreNocase>  set1   ;  即可

struct CompareNocase{
	bool operator()(const string&str1 , congst string & str2){
		string str_1;
		str_1.resize(str1.size());
		transform(str1.begin(),str2.begin(),str_1.begin(), tolower) // tolower是預定於函式Udin物件 
			bool operator()(const string&str1 , congst string & str2){
		string str_2;
		str_2.resize(str2.size());
		transform(str2.begin(),str2.begin(),str_2.begin(), tolower) // tolower是預定於函式Udin物件
	}
};