For_each loop issue

why the syntax written in the webinar is not working in codeblocks?
but the syntax
for_each(s.begin(),s.end(),print1);
is working?

Hello @Rj.25

Please mention the syntax given in the webinar that is Not working in codeblocks.
Also mention the time in the webinar at which the syntax is given.

another question :can you explain how comparator works for binary search?

Hello @Rj.25

Comparator is a function which takes as input two parameters of same data type and tells whether the first should come before or after the second parameter in sorted order.
Example:
we have an array of strings arr[]: “apple”, “banana”, “sky”, “home”.
if we would like to sort the above strings on the basis of their length i.e., greater length comes first then we can say sort(arr, arr + arr.size(), compare);

where compare function is

bool compare(string a, string b) {
    if(a.length() > b.length()) {
        return true;
    } else {
        return false;
    }
}

Here the sort function uses the “comapre” comparator to compare two strings (whether the first should come before or after the second string in sorted order).
comparator is just a function which tells the sort function the basis for sorting some objects.

Let me know if you need any other help.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.