东方pos机电源适配器批发,实例理解C++ STL函数对象适配器

 新闻资讯2  |   2023-05-25 11:26  |  投稿人:pos机之家

网上有很多关于东方pos机电源适配器批发,实例理解C++ STL函数对象适配器的知识,也有很多人为大家解答关于东方pos机电源适配器批发的问题,今天pos机之家(www.poszjia.com)为大家整理了关于这方面的知识,让我们一起来看下吧!

本文目录一览:

1、东方pos机电源适配器批发

东方pos机电源适配器批发

看以下transform()算法的声明,需要使用函数对象或函数指针。根据参数的不同,重载了三个版本,函数对象或函数指针有使用一个参数(UnaryOperation)的,也有使用两个参数(BinaryOperation)的。

unary operation(1)template <class Inputiterator, class OutputIterator, class UnaryOperation> OutputIterator transform (InputIterator first1, InputIterator last1, OutputIterator result, UnaryOperation op);template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation> OutputIterator transform (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator result, BinaryOperation binary_op);template <class InputIterator, class OutputIterator, class UnaryOperator> OutputIterator transform (InputIterator first1, InputIterator last1, OutputIterator result, UnaryOperator op){ while (first1 != last1) { *result = op(*first1); // or: *result=binary_op(*first1,*first2++); ++result; ++first1; } return result;}

通常,函数对象或函数指针的参数来源于此参数前面的参数(容器容器迭代器)。看以下实例:

#include <deque>#include <algorithm>#include <functional>#include <iostream>using namespace std;template <typename T>inline void PRINT_ELEMENTS (const T& coll, const std::string& optstr=""){ std::cout << optstr; typename T::const_iterator pos; // not static for (pos=coll.begin(); pos!=coll.end(); ++pos) { std::cout << *pos << ' '; //for (const auto& elem : coll) { // c++11 //std::cout << elem << ’ ’; } std::cout << std::endl;}int main(){ deque<int> coll = { 1, 2, 3, 5, 7, 11, 13, 17, 19 }; PRINT_ELEMENTS(coll,"initialized: "); // negate all values in coll transform (coll.cbegin(),coll.cend(), // source coll.begin(), // destination negate<int>()); // operation PRINT_ELEMENTS(coll,"negated: "); // square all values in coll transform (coll.cbegin(),coll.cend(), // first source coll.cbegin(), // second source coll.begin(), // destination multiplies<int>()); // operation PRINT_ELEMENTS(coll,"squared: ");}/*initialized: 1 2 3 5 7 11 13 17 19 negated: -1 -2 -3 -5 -7 -11 -13 -17 -19 squared: 1 4 9 25 49 121 169 289 361 */

以下是使用两个参数的函数对象模板:

template <class T> struct multiplies : binary_function <T,T,T> { T operator() (const T& x, const T& y) const {return x*y;}};

multiplies函数对象使用的两个参数,在caller transform算法调用时,由caller transform的实参(迭代器)给multiples传递实参:

*result=binary_op(*first1,*first2++);

You can use special function adapters, or so-called binders, to combine predefined function objects with other values or use special cases(如让其中的一个参数绑定到常量). Here is a complete example:

#include <set>#include <deque>#include <algorithm>#include <iterator>#include <functional>#include <iostream>using namespace std;using namespace std::placeholders; // adds visibility of _1, _2, _3,...template <typename T>inline void PRINT_ELEMENTS (const T& coll, const std::string& optstr=""){ std::cout << optstr; typename T::const_iterator pos; // not static for (pos=coll.begin(); pos!=coll.end(); ++pos) { std::cout << *pos << ' '; //for (const auto& elem : coll) { // c++11 //std::cout << elem << ’ ’; } std::cout << std::endl;}int main(){ set<int,greater<int>> coll1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; deque<int> coll2; // Note: due to the sorting criterion greater<>() elements have reverse order: PRINT_ELEMENTS(coll1,"initialized: "); // transform all elements into coll2 by multiplying them with 10 transform (coll1.cbegin(),coll1.cend(), // source back_inserter(coll2), // destination bind(multiplies<int>(),_1,10)); // function adatpor___________________ PRINT_ELEMENTS(coll2,"transformed: "); // replace value equal to 70 with 42 replace_if (coll2.begin(),coll2.end(), // range bind(equal_to<int>(),_1,70), // function adatpor___________________ 42); // new value PRINT_ELEMENTS(coll2,"replaced: "); // remove all elements with values between 50 and 80 coll2.erase(remove_if(coll2.begin(),coll2.end(), bind(logical_and<bool>(),// function adatpor___________________ bind(greater_equal<int>(),_1,50),// function adatpor___________________ bind(less_equal<int>(),_1,80))),// function adatpor___________________ coll2.end()); PRINT_ELEMENTS(coll2,"removed: ");}/*initialized: 9 8 7 6 5 4 3 2 1 transformed: 90 80 70 60 50 40 30 20 10 replaced: 90 80 42 60 50 40 30 20 10 removed: 90 42 40 30 20 10 */

再一个实例了解binder():

#include <iostream> // std::cout#include <functional> // std::bind// a function: (also works with function object: std::divides<double> my_divide;)double my_divide (double x, double y) {return x/y;}struct MyPair { double a,b; double multiply() {return a*b;}};int main () { using namespace std::placeholders; // adds visibility of _1, _2, _3,... // binding functions: auto fn_five = std::bind (my_divide,10,2); // returns 10/2 std::cout << fn_five() << '\'; // 5 auto fn_half = std::bind (my_divide,_1,2); // returns x/2 std::cout << fn_half(10) << '\'; // 5 auto fn_invert = std::bind (my_divide,_2,_1); // returns y/x std::cout << fn_invert(10,2) << '\'; // 0.2 auto fn_rounding = std::bind<int> (my_divide,_1,_2); // returns int(x/y) std::cout << fn_rounding(10,3) << '\'; // 3 MyPair ten_two {10,2}; // binding members: auto bound_member_fn = std::bind (&MyPair::multiply,_1); // returns x.multiply() std::cout << bound_member_fn(ten_two) << '\'; // 20 auto bound_member_data = std::bind (&MyPair::a,ten_two); // returns ten_two.a std::cout << bound_member_data() << '\'; // 10 return 0;}

ref:

Nicolai M. Josuttis 《The C++ Standard Library》

-End-

以上就是关于东方pos机电源适配器批发,实例理解C++ STL函数对象适配器的知识,后面我们会继续为大家整理关于东方pos机电源适配器批发的知识,希望能够帮助到大家!

转发请带上网址:http://www.poszjia.com/newsone/54868.html

你可能会喜欢:

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 babsan@163.com 举报,一经查实,本站将立刻删除。