Doubt in a quiz question

Hello, this is the question in which I am facing problem. I just want to know how the comparator will work and how the strings are passed as s1 and s2 from vector ?

Given the following code snippet :

bool comp(string s1, string s2)
{
if(s1.length() < s2.length())
return 1;
else if(s1.length() > s2.length())
return 0;
else return s1 < s2;
}

vector< string > data = {“b”, “a”, “c”, “abc”, “bca”, “xy”};
sort(data.begin(), data.end(), comp);
for(string item : data)
cout << item << " ";

Choose the correct output :

Hello @yashsharma4304 in this comparator function int the first if else /we want to sort it in the descending order on the basis of their lengths.
and if bydefault the length of both the strings are same then we will sort them on the basis of the first letter.
if you have any other doubt you can ask here:
Happy Learning!!

1 Like

ok thanks for your support. It means alot for me :slightly_smiling_face:

Hello @yashsharma4304 happy to help you!
you can now mark this doubt as resolved.
Happy Learning!!

1 Like

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.

1 Like