関数オブジェクト/組み込み関数オブジェクト
43254 ワード
関数オブジェクト
概念:リロード関数呼び出しオペレータのクラスで、そのオブジェクトはよく関数オブジェクト関数オブジェクトと呼ばれ、リロード()を使用する場合、動作は関数呼び出しに似ており、シミュレーション関数とも呼ばれます.
本質:関数オブジェクト(シミュレーション関数)はクラスであり、関数ではありません.
関数オブジェクトの使用
特徴:関数オブジェクトは使用する時、普通の関数のように呼び出すことができて、パラメータがあってもよくて、戻り値の関数オブジェクトが普通の関数を超える概念があってもよくて、関数オブジェクトは自分の状態の関数オブジェクトがあってパラメータとして伝達することができます
例:
まとめ:シミュレーション関数の書き方は非常に柔軟で、パラメータとして伝達することができます.
述語
コンセプト:boolタイプを返すシミュレーション関数を述語と呼びます.operator()がパラメータを受け入れる場合は、一元述語と呼ばれます.operator()が2つのパラメータを受け入れる場合は、二元述語と呼ばれます.
一元述語の例:
二元述語の例:
まとめ:パラメータは2つの述語のみで、二元述語と呼ばれます.
ビルド関数
コンセプト:STLにはいくつかの関数オブジェクトが作成されています
分類:算術シミュレーション関数関係シミュレーション関数論理シミュレーション関数
使用法:これらの擬似関数によって生成されるオブジェクトは、一般関数とまったく同じ使用法で組み込み関数オブジェクトを使用し、ヘッダファイルincludeを導入する必要があります.
さんじゅつシミュレーションかんすう
機能説明:四則演算を実現し、negateは一元演算であり、その他は二元演算シミュレーション関数の原型である.
例:
まとめ:組み込み関数オブジェクトを使用する場合は、ヘッダファイル#includeを導入する必要があります
かんけいシミュレーションかんすう
機能の説明:関係比較シミュレーション関数のプロトタイプを実現する:
例:
まとめ:リレーショナルシミュレーション関数でよく使われるのはgreater<>が
ろんりシミュレーションかんすう
機能の説明:論理演算関数のプロトタイプを実現する:
例:
概念:リロード関数呼び出しオペレータのクラスで、そのオブジェクトはよく関数オブジェクト関数オブジェクトと呼ばれ、リロード()を使用する場合、動作は関数呼び出しに似ており、シミュレーション関数とも呼ばれます.
本質:関数オブジェクト(シミュレーション関数)はクラスであり、関数ではありません.
関数オブジェクトの使用
特徴:関数オブジェクトは使用する時、普通の関数のように呼び出すことができて、パラメータがあってもよくて、戻り値の関数オブジェクトが普通の関数を超える概念があってもよくて、関数オブジェクトは自分の状態の関数オブジェクトがあってパラメータとして伝達することができます
例:
#include
#include
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()
{
count = 0;
}
void operator()(string test)
{
cout << test << endl;
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 &mp , string test)
{
mp(test);
}
void test03()
{
MyPrint myPrint;
doPrint(myPrint, "Hello C++");
}
int main()
{
//test01();
//test02();
test03();
return 0;
}
まとめ:シミュレーション関数の書き方は非常に柔軟で、パラメータとして伝達することができます.
述語
コンセプト:boolタイプを返すシミュレーション関数を述語と呼びます.operator()がパラメータを受け入れる場合は、一元述語と呼ばれます.operator()が2つのパラメータを受け入れる場合は、二元述語と呼ばれます.
一元述語の例:
#include
#include
#include
using namespace std;
//1.
class GreaterFive
{
bool operator()(int val)
{
return val > 5;
}
};
void test01()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
if (it == v.end())
{
cout << " !" << endl;
}
else
{
cout << " :" << *it << endl;
}
}
int main()
{
test01();
return 0;
}
二元述語の例:
#include
#include
#include
using namespace std;
//
class MyCompare
{
public:
bool operator()(int num1, int num2)
{
return num1 > num2;
}
};
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;
cout << "----------------------------" << endl;
// ,
sort(v.begin(), v.end(), MyCompare());
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test01();
return 0;
}
まとめ:パラメータは2つの述語のみで、二元述語と呼ばれます.
ビルド関数
コンセプト:STLにはいくつかの関数オブジェクトが作成されています
分類:算術シミュレーション関数関係シミュレーション関数論理シミュレーション関数
使用法:これらの擬似関数によって生成されるオブジェクトは、一般関数とまったく同じ使用法で組み込み関数オブジェクトを使用し、ヘッダファイルincludeを導入する必要があります.
さんじゅつシミュレーションかんすう
機能説明:四則演算を実現し、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> //
例:
#include
#include
using namespace std;
//negate
void test01()
{
negate<int> n;
cout << n(50) << endl;
}
//plus
void test02()
{
plus<int> p;
cout << p(10, 20) << endl;
}
int main()
{
test01();
test02();
return 0;
}
まとめ:組み込み関数オブジェクトを使用する場合は、ヘッダファイル#includeを導入する必要があります
かんけいシミュレーションかんすう
機能の説明:関係比較シミュレーション関数のプロトタイプを実現する:
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> //
例:
#include
#include
#include
#include
using namespace std;
class MyCompare
{
public:
bool operator()(int v1,int v2)
{
return v1 > v2;
}
};
void test01()
{
vector<int> v;
v.push_back(10);
v.push_back(30);
v.push_back(50);
v.push_back(40);
v.push_back(20);
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
//
//sort(v.begin(), v.end(), MyCompare());
//STL
sort(v.begin(), v.end(), greater<int>());
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test01();
return 0;
}
まとめ:リレーショナルシミュレーション関数でよく使われるのはgreater<>が
ろんりシミュレーションかんすう
機能の説明:論理演算関数のプロトタイプを実現する:
template<class T> bool logical_and<T> //
template<class T> bool logical_or<T> //
template<class T> bool logical_not<T> //
例:
#include
#include
#include
#include
using namespace std;
void test01()
{
vector<bool> v;
v.push_back(true);
v.push_back(false);
v.push_back(true);
v.push_back(false);
for (vector<bool>::iterator it = v.begin();it!= v.end();it++)
{
cout << *it << " ";
}
cout << endl;
// v v2 ,
vector<bool> v2;
v2.resize(v.size());
transform(v.begin(), v.end(), v2.begin(), logical_not<bool>());
for(vector<bool>::iterator it = v2.begin(); it != v2.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test01();
return 0;
}