東方pos機(jī)電源適配器批發(fā),實(shí)例理解C++ STL函數(shù)對(duì)象適配器

 新聞資訊2  |   2023-05-25 11:26  |  投稿人:pos機(jī)之家

網(wǎng)上有很多關(guān)于東方pos機(jī)電源適配器批發(fā),實(shí)例理解C++ STL函數(shù)對(duì)象適配器的知識(shí),也有很多人為大家解答關(guān)于東方pos機(jī)電源適配器批發(fā)的問(wèn)題,今天pos機(jī)之家(m.afbey.com)為大家整理了關(guān)于這方面的知識(shí),讓我們一起來(lái)看下吧!

本文目錄一覽:

1、東方pos機(jī)電源適配器批發(fā)

東方pos機(jī)電源適配器批發(fā)

看以下transform()算法的聲明,需要使用函數(shù)對(duì)象或函數(shù)指針。根據(jù)參數(shù)的不同,重載了三個(gè)版本,函數(shù)對(duì)象或函數(shù)指針有使用一個(gè)參數(shù)(UnaryOperation)的,也有使用兩個(gè)參數(shù)(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;}

通常,函數(shù)對(duì)象或函數(shù)指針的參數(shù)來(lái)源于此參數(shù)前面的參數(shù)(容器容器迭代器)??匆韵聦?shí)例:

#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 */

以下是使用兩個(gè)參數(shù)的函數(shù)對(duì)象模板:

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

multiplies函數(shù)對(duì)象使用的兩個(gè)參數(shù),在caller transform算法調(diào)用時(shí),由caller transform的實(shí)參(迭代器)給multiples傳遞實(shí)參:

*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(如讓其中的一個(gè)參數(shù)綁定到常量). 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 */

再一個(gè)實(shí)例了解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-

以上就是關(guān)于東方pos機(jī)電源適配器批發(fā),實(shí)例理解C++ STL函數(shù)對(duì)象適配器的知識(shí),后面我們會(huì)繼續(xù)為大家整理關(guān)于東方pos機(jī)電源適配器批發(fā)的知識(shí),希望能夠幫助到大家!

轉(zhuǎn)發(fā)請(qǐng)帶上網(wǎng)址:http://m.afbey.com/newsone/54868.html

你可能會(huì)喜歡:

版權(quán)聲明:本文內(nèi)容由互聯(lián)網(wǎng)用戶自發(fā)貢獻(xiàn),該文觀點(diǎn)僅代表作者本人。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如發(fā)現(xiàn)本站有涉嫌抄襲侵權(quán)/違法違規(guī)的內(nèi)容, 請(qǐng)發(fā)送郵件至 babsan@163.com 舉報(bào),一經(jīng)查實(shí),本站將立刻刪除。