头文件
#include
1. bind
示例一
void useBindDemo() {
set myset;
myset.insert(3);
myset.insert(4);
myset.insert(5);
vector vec;
transform(myset.begin(), myset.end(), back_inserter(vec), bind(multiplies(), placeholders::_1, 10));
copy(vec.begin(), vec.end(), ostream_iterator(cout, " "));
}
示例二
//convert a regular function into functor
double Pow(double x, double y) {
return pow(x, y);
}
void useBindDemo2() {
set myset;
myset.insert(3);
myset.insert(4);
myset.insert(5);
deque d;
//vector d;
auto f = function(Pow);
transform(myset.begin(), myset.end(), back_inserter(d), bind(f, placeholders::_1, 2));
// d {9,16,25}
}
将某个容器中某个区间范围的数属于哪个范围
void useBindDemo3() {
set myset;
for (int t = 0; t < 20; t++) {
myset.insert(t);
}
vector d;
transform(myset.begin(), myset.end(), back_inserter(d),
bind(logical_and(),
bind(greater(), placeholders::_1, 2),
bind(less(), placeholders::_1, 10)));
//d {0 0 0 1 1 1 1 1 1 0 ......}
copy(d.begin(), d.end(), ostream_iterator(cout, " "));
}
将某个范围的数拷贝到容器中
bool needCopy(int x) {
return (x > 2 && x < 10);
}
void useBindDemo4() {
set myset;
for (int t = 0; t < 20; t++) {
myset.insert(t);
}
vector d;
transform(myset.begin(), myset.end(), back_inserter(d),needCopy);
}
这里我们发现,这拷贝进去的是 0 1 啊,不是我们想要的啊。那我们应该修改啊。自己尝试下吧。
2. lambda
void useLambda() {
set myset;
for (int t = 0; t < 20; t++) {
myset.insert(t);
}
vector d;
transform(myset.begin(), myset.end(), back_inserter(d),
[](int x) {return ((x > 2) && (x < 10)); });
}