I have asked a related question before, and it was resolved successfully. In the past, when I wanted to use std::max_element in order to find the maximum element (or even sort by using std::sort) of a vector of structuresaccording to one of the members of the structure, all I had to do was to insert a specially designed comparison function as the third argument of the function std::max::element. But the latter comparison function naturally accepts two arguments internally.

For instance, here is a test program that successfully finds the maximum according to just one member of the structure:

And the output was this, as expected:Code:#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; struct S { string myWord; int a; int b; }; int main() { vector<S> vec; for(int j=0; j<10; j++) { //Initialize: S tempS; tempS.a = j; tempS.b = 5+j; tempS.myWord = "aWord"; vec.push_back(tempS); } std::vector<S>::iterator result; result = std::max_element(vec.begin(), vec.end(), [](const S & S_0, const S & S_1) { return ( S_0.a < S_1.a ) ; } ) ; cout << "Maximum element S.a of vector<S> vec is at: " << std::distance(vec.begin(), result) << endl; cout << "---------------------------------------------" << endl; size_t range = 3 ; size_t index_max; for(vector<S>::iterator itr=vec.begin( ) + 6 ; itr != vec.end( ) ; itr++) { size_t index = itr - vec.begin(); vector<S>::iterator first = itr -range; //std::max_element(first, last) returns an answer between [first,last) as half-open interval //and so we need to further increase last=itr+1 as follows // (or else the last element does not get compared): vector<S>::iterator last = itr + 1 ; // result = std::max_element( first , last, CompForMax_a ); result = std::max_element( first , last, [](const S & S_0, const S & S_1) { return ( S_0.a < S_1.a ) ; } ); index_max = std::distance(vec.begin(), result); cout << "max element of vec[i].a between slot " << index -range << " and slot " << index << " is: " << (*result).a << ", and its index is: " << index_max << endl; cout << "vec[" << index << "].a = " << (*itr).a << endl; } }

Maximum element S.a of vector<S> vec is at: 9

---------------------------------------------

[I]max element of vec.a between slot 3 and slot 6 is: 6, and its index is: 6

vec[6].a = 6

[I]max element of vec.a between slot 4 and slot 7 is: 7, and its index is: 7

vec[7].a = 7

[I]max element of vec.a between slot 5 and slot 8 is: 8, and its index is: 8

vec[8].a = 8

[I]max element of vec.a between slot 6 and slot 9 is: 9, and its index is: 9

vec[9].a = 9

-------------------------------------------------------

However, I now need to search and find an element of vector<myStruct> according to just one member of myStruct, instead of finding the maximum or sorting as before. This presents a problem because the function std::find does not accept such a comparison function as its third argument.

This was the description of the std::find function that I found:

find - C++ Reference

I could also find another function called std::find_if, but this only accepts a unary predicate like this:Code:`template <class InputIterator, class T> InputIterator find (InputIterator first, InputIterator last, const T& val);`

find_if - C++ Reference

And once again this is either inadequate of I don't see how to use it directly, because for the third argument I would like to insert a function that takes two arguments with a syntax like this:Code:template <class InputIterator, class UnaryPredicate> InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);

Code:int x=7; std::vector<S>::iterator result; result = std::find(vec.begin(), vec.end(), []( const (int x, const S & S_1) { return ( x == S_1.a ) ; } ) ;

Is there another std function that I can use to make search and find an element according to just one member of myStruct?

Or perhaps there is a clever way to pass two arguments to the unary predicate.

Many thanks.