STLの二元関数binary_function

1654 ワード


関数オブジェクトはoperator()を再ロードしたクラスのインスタンスであり、operator()は関数呼び出し演算子です.
標準C++ライブラリはoperator()パラメータの個数によって区分され、主に以下の種類があります.
1ジェネレータ:パラメータがなく、任意のタイプの値を返す関数オブジェクト、たとえば乱数ジェネレータ.
2単元関数:任意のタイプのパラメータが1つしかなく、異なるタイプの値を返す関数オブジェクト.
3二元関数:2つの任意のタイプのパラメータがあり、異なるタイプの値が可能な関数オブジェクトを返します.
4一元判定関数:bool型値を返す一元関数.
5二元判定関数:bool型値を返す二元関数.
サンプル
//1カスタムタイプ
     
#include 
#include 
using namespace std;

class Student
{
public:
	Student(void);
	~Student(void);

public:
	string _stuName;
	float  _stuScore;

public:
	Student(string stuName,float stuScore)
	{
		_stuName=stuName;
		_stuScore=stuScore;
	}

	//       
	bool operator 
 
//2リロードシステムの二元関数
#pragma once
#include 
using namespace std;


//       
template 
class binary_sort:public binary_function
{
public:
	bool operator()(inPara in1,outPara in2)
	{
		return in1

//3使用例
 
#include "stdafx.h"

#include 
#include 
#include 
#include 
#include 

#include "Student.h"
#include "binary_sort.h"


int _tmain(int argc, _TCHAR* argv[])
{

	vectorv;

	//    
	Student s1("11111",67);
	v.push_back(s1);

	Student s2("2222",45);
	v.push_back(s2);

	Student s3("33333",78);
	v.push_back(s3);

   //      ,      
   cout<());

   for_each(v.begin(),v.end(),Student());


	getchar();
	return 0;
}