C++相关闲碎记录(8)
2023-12-13 05:34:11
1、预定义的Function adapter 和 binder
#include <iostream>
#include <functional>
int main() {
auto plus10 = std::bind(std::plus<int>(), std::placeholders::_1, 10);
std::cout << "+10: " << plus10(6) << std::endl;
auto plus10times2 = std::bind(std::multiplies<int>(),
std::bind(std::plus<int>(),
std::placeholders::_1,
10),
2);
std::cout << "+10 *2: " << plus10times2(7) << std::endl;
auto pow3 = std::bind(std::multiplies<int>(),
std::bind(std::multiplies<int>(),
std::placeholders::_1,
std::placeholders::_1),
std::placeholders::_1);
std::cout << "x*x*x: " << pow3(7) << std::endl;
auto inverseDivide = std::bind(std::divides<double>(),
std::placeholders::_2,
std::placeholders::_1);
std::cout << "invdiv: " << inverseDivide(49, 7) << std::endl;
}
输出:
+10: 16
+10 *2: 34
x*x*x: 343
invdiv: 0.142857
2、bind调用全局函数
#include <iostream>
#include <algorithm>
#include <functional>
#include <locale>
#include <string>
using namespace std;
using namespace std::placeholders;
char myToupper(char c) {
std::locale loc;
return std::use_facet<std::ctype<char>> (loc).toupper(c);
}
int main() {
string s("Internationalization");
string sub("Nation");
// search substring case insensitive
string::iterator pos;
pos = search(s.begin(), s.end(),
sub.begin(), sub.end(),
bind(equal_to<char>(),
bind(myToupper, _1),
bind(myToupper, _2)));
if (pos != s.end()) {
std::cout << "\"" << sub << "\" is part of \"" << s << "\"" << std::endl;
}
return 0;
}
输出:
"Nation" is part of "Internationalization"
?3、bind调用成员函数
#include <functional>
#include <algorithm>
#include <vector>
#include <iostream>
#include <string>
using namespace std;
using namespace std::placeholders;
class Person {
private:
string name;
public:
Person(const string& n):name(n) {}
void print() const {
cout << name << endl;
}
void print2 (const string& prefix) const {
cout << prefix << name << endl;
}
};
int main() {
vector<Person> coll = {Person("Tick"), Person("Trick"), Person("Track")};
for_each(coll.begin(), coll.end(), bind(&Person::print,_1)); //param.print()
cout << endl;
for_each(coll.begin(), coll.end(), bind(&Person::print2, _1, "Person: ")); //param.print2("Person: ")
cout << endl;
bind(&Person::print2, _1, "This is: ")(Person("nico")); // (Person("nico")).print2("This is: ")
return 0;
}
输出:
Tick
Trick
Track
Person: Tick
Person: Trick
Person: Track
This is: nico
?也可以传递pointer object 或者smart pointer 给bind()
std::vector<Person*> cp;
std::for_each(cp.begin(), cp.end(), std::bind(&Person::print,
std::placeholders::_1));
std::vector<std::shared_ptr<Person>> sp;
std::for_each(sp.begin(), sp.end(), std::bind(&Person::print,
std::placeholders::_1));
4、mem_fn() Adapter
对于成员函数,可以改用mem_fn() adapter,就不在需要使用占位符表示调用者了。
std::for_each(coll.begin(), coll.end(), std::mem_fn(&Person::print));
如有额外的实参被传递给成员函数,mem_fn()就拿其中的第一实参作为调用者,其他实参当做成员函数的实参。
std::mem_fn(&Person::print)(n); // calls n.print()
std::mem_fn(&Person::print2)(n, "Person: "); // calls n.print2("Person: ");
然而,如果要为Function object绑定额外的实参,还是必须使用bind()
std::for_each(coll.begin(), coll.end(),
std::bind(std::mem_fn(&Person::print2),
std::placeholders::_1,
"Person: "));
5、绑定至数据成员
map<string, int> coll;
int sum = accumulate(coll.begin(), coll.end(),
0,
std::bind(plus<int>(),
std::placeholders::_1,
std::bind(&map<string,int>::value_type::second,
std::placeholders::_2)));
?bind(&map<string,int>::value_type::second, _2) 把每次调用这个predicate所传入的第二实参绑定为数据成员second。
6、adapter not1() 和 not2()
adapter not1() 和 not2() 几乎可被认为是过时的,他们唯一用途就是令预定义的Function object的意义相反,例如:
std::sort(coll.begin(), coll.end(),
std::not2(std::less<int>())));
?这看起来比下面简单多了:
std::sort(coll.begin(), coll.end(),
std::bind(std::logical_not<bool>(),
std::bind(std::less<int>(), _1, _2)));
然而实际中并没有not1()和not2()发挥的作用的地方,因为你可以轻松的选择另外一个预定义的Function object
std::sort(coll.begin(), coll.end(),
std::greater_equal<int>());
请注意,not2()搭配less<>其实是错误的,<的相反是>=,不满足反对称条件,所以要么传递greater<int>(),要么传递bind(less<int>(), _2, _1)
7、function adapter 搭配用户自定义的Function object
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
#include <iterator>
using namespace std;
using namespace std::placeholders;
template <typename T1, typename T2>
struct fopow {
T1 operator() (T1 base, T2 exp) const {
return std::pow(base, exp);
}
};
int main() {
vector<int> coll = {1, 2, 3, 4, 5, 6, 7, 8, 9};
transform(coll.begin(), coll.end(),
ostream_iterator<float>(cout," "),
bind(fopow<float, int>(), 3, _1));
cout << endl;
transform(coll.begin(), coll.end(),
ostream_iterator<float>(cout, " "),
bind(fopow<float, int>(), _1, 3));
cout << endl;
return 0;
}
输出:
3 9 27 81 243 729 2187 6561 19683
1 8 27 64 125 216 343 512 729
8、过时的Function adapter
?9、Lambda带有状态的Function object
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> coll = {1, 2, 3, 4, 5, 6, 7, 8};
long sum = 0;
for_each(coll.begin(), coll.end(), [&sum](int elem){
sum += elem;
});
double w = static_cast<double>(sum) / static_cast<double>(coll.size());
cout << "mean value: " << w << endl;
return 0;
}
输出:
mean value: 4.5
文章来源:https://blog.csdn.net/wj617906617/article/details/134928888
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!