算法无论在什么地方都要求一个类似函数的实体
系统可以提供一个指向普通函数或是一个函数对象的指针
当算法通过函数指针调用时
就启用了本地的函数调用机制用ptr_fun() 来封装一个一元函数
//: C06:PtrFun1.cpp
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
// Using ptr_fun() with a unary function.
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
int d[] = { 123, 94, 10, 314, 315 };
const int DSZ = sizeof d / sizeof *d;
bool isEven(int x) { return x % 2 == 0; }
int main() {
vector<bool> vb;
transform(d, d + DSZ, back_inserter(vb),
not1(ptr_fun(isEven)));
copy(vb.begin(), vb.end(),
ostream_iterator<bool>(cout, " "));
cout << endl;
// Output: 1 0 0 0 1
getchar();
} ///:~
输出
1 0 0 0 1
不能仅把isEven传递给not1
因为not1需要知道它使用的实际参数的类型和返回值类型
使用ptr_fun()的二元版本来增加序列中的乘方个数
在向ptr_fun()传递重载函数时暴露一个缺陷
//: C06:PtrFun2.cpp {-edg}
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
// Using ptr_fun() for a binary function.
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
double d[] = { 01.23, 91.370, 56.661,
023.230, 19.959, 1.0, 3.14159 };
const int DSZ = sizeof d / sizeof *d;
int main() {
vector<double> vd;
transform(d, d + DSZ, back_inserter(vd),
bind2nd(ptr_fun<double, double, double>(pow), 2.0));
copy(vd.begin(), vd.end(),
ostream_iterator<double>(cout, " "));
cout << endl;
getchar();
} ///:~
输出
1.5129 8348.48 3210.47 539.633 398.362 1 9.86959
因为有多重pow()版本
编译器不知道选择哪一个
需要借助显式的函数模板特化来帮助编译器
用通用算法将一个成员函数转化为适合使用的函数对象更巧妙
假定有一个圆形问题
并且相对Shape容器内的每个指针都应用draw()成员函数
//: C06:MemFun1.cpp
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
// Applying pointers to member functions.
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
#include "../purge.h"
using namespace std;
class Shape {
public:
virtual void draw() = 0;
virtual ~Shape() {}
};
class Circle : public Shape {
public:
virtual void draw() { cout << "Circle::Draw()" << endl; }
~Circle() { cout << "Circle::~Circle()" << endl; }
};
class Square : public Shape {
public:
virtual void draw() { cout << "Square::Draw()" << endl; }
~Square() { cout << "Square::~Square()" << endl; }
};
int main() {
vector<Shape*> vs;
vs.push_back(new Circle);
vs.push_back(new Square);
for_each(vs.begin(), vs.end(), mem_fun(&Shape::draw));
purge(vs);
getchar();
} ///:~
输出
Circle::Draw()
Square::Draw()
Circle::~Circle()
Square::~Square()
for_each()算法将序列中每一个元素依次传递给由第3个参数指示的函数对象
希望函数对象封装成它自身类的一个成员函数
假设有一个对象的容器
现在想调用一个参数的成员函数
传递的参数来自对象的第2个容器
//: C06:MemFun2.cpp
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
// Calling member functions through an object reference.
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
class Angle {
int degrees;
public:
Angle(int deg) : degrees(deg) {}
int mul(int times) { return degrees *= times; }
};
int main() {
vector<Angle> va;
for(int i = 0; i < 50; i += 10)
va.push_back(Angle(i));
int x[] = { 1, 2, 3, 4, 5 };
transform(va.begin(), va.end(), x,
ostream_iterator<int>(cout, " "),
mem_fun_ref(&Angle::mul));
cout << endl;
// Output: 0 20 60 120 200
getchar();
} ///:~
输出
0 20 60 120 200
因为容器持有对象
所以必须通过成员函数指针来使用mem_fun_ref()
transform()使用第1个范围的开始和结束点
第2个范围的开始点大多数任意类型的成员函数与mem_fun_ref()一起工作
如果用户使用的编译器没有增加任何一个默认参数而超过标准库中指定的正规参数
也可以使用标准库成员函数
//: C06:FindBlanks.cpp
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
// Demonstrates mem_fun_ref() with string::empty().
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <fstream>
#include <functional>
#include <string>
#include <vector>
#include "../require.h"
using namespace std;
typedef vector<string>::iterator LSI;
int main(int argc, char* argv[]) {
char* fname = "FindBlanks.cpp";
if(argc > 1) fname = argv[1];
ifstream in(fname);
assure(in, fname);
vector<string> vs;
string s;
while(getline(in, s))
vs.push_back(s);
vector<string> cpy = vs; // For testing
LSI lsi = find_if(vs.begin(), vs.end(),
mem_fun_ref(&string::empty));
while(lsi != vs.end()) {
*lsi = "A BLANK LINE";
lsi = find_if(vs.begin(), vs.end(),
mem_fun_ref(&string::empty));
}
for(size_t i = 0; i < cpy.size(); i++)
if(cpy[i].size() == 0)
assert(vs[i] == "A BLANK LINE");
else
assert(vs[i] != "A BLANK LINE");
} ///:~
无异常对话框弹出
使用find_if() mem_fun_ref()和string::empty()一起
在指定范围的序列中查找第1个空白行的位置