c++ - std::bind2nd and std::bind with bidimensional arrays and arrays of structs -


i know c++ have lambdas , std::bind1st, std::bind2nd , std::bind deprecated.

however, start foundations of c++, can understand better new features.

so, start simple code, using array of ints:

first example: std::bind2nd

int array1[] = { 10, 20, 30, 40, 50, 60, 40 }; int c1, c2, c3;  c1 = count_if(array1, array1 + 7, bind2nd(greater<int>(), 40)); c2 = count_if(array1, array1 + 7, bind2nd(less<int>(), 40)); c3 = count_if(array1, array1 + 7, bind2nd(equal_to<int>(), 40)); cout << "there " << c1 << " elements greater 40." << endl; cout << "there " << c2 << " elements lesser 40." << endl; cout << "there " << c3 << " elements equal 40." << endl; 

second example: std::bind

greater<int> big; less<int> small; equal_to<int> equal;  c1 = count_if(array1, array1 + 7, bind(big, _1, 40)); c2 = count_if(array1, array1 + 7, bind(small, _1, 40)); c3 = count_if(array1, array1 + 7, bind(equal, _1, 40)); cout << "there " << c1 << " elements greater 40." << endl; cout << "there " << c2 << " elements lesser 40." << endl; cout << "there " << c3 << " elements equal 40." << endl; 

in both cases output is:

there 2 elements greater 40. there 3 elements lesser 40. there 2 elements equal 40. 

how can same bidimentional arrays below:
(i want made same operations 2nd coordinate)

int array2[7][2] = { { 1, 10 }, { 2, 20 }, { 3, 30 },                       { 4, 40 }, { 5, 50 }, { 6, 60 }, { 4, 40 } }; 

and arrays of structs this:

struct st {     char c;     int i; };  st array3[] = { { 'a', 10 }, { 'b', 20 }, { 'c', 30 },                  { 'd', 40 }, { 'e', 50 }, { 'f', 60 }, { 'd', 40 } }; 

in case, want same operations field 'int' in array of structs.

can me?

thank you

bind1st, bind2nd , brethren deprecated in c++11 , outright removed in c++17. in case didn't know this.

with bind, solution straightforward, can use fact bind expressions composable , can use bind extract data member (placeholders omitted brevity):

auto gr = count_if(array3, array3 + 7, bind(greater<>{}, bind(&st::i, _1), 40)); auto ls = count_if(array3, array3 + 7, bind(less<>{}, bind(&st::i, _1), 40)); auto eq = count_if(array3, array3 + 7, bind(equal_to<>{}, bind(&st::i, _1), 40)); 

with bind2nd it's not easy. need declare function object (can't use function) has several typedefs. can use binary_function ease this:

struct my_greater : binary_function<st, int, bool> {     bool operator()(st const& l, int r) const {         return greater<>{}(l.i, r);     } }; 

then can call

auto old = count_if(array3, array3 + 7, bind2nd(my_greater{}, 40)); 

in c++11 cam employ lambdas:

auto xi = count_if(array3, array3 + 7, [](st const& l){ return l.i > 40}); 

demo of all


if have c++11 or newer available, it's better choice use lambdas. it's not "good default", you'd have contort situation bind better solution.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -