Please explain the code and the output
Hey @gargshivam2001 so i will divide your code in three parts for better understanding. Before doing that, a brief idea about it is you are having a vector of strings, you have sorted it and then you have printed your vector
Sorting function into 3 parts:
- Suppose you have 2 different strings x & y.
if(s1.length() < s2.length())
return 1;
This means that if your x length is lesser then y length. You will send true. Which means you are sorting them in increasing order of length
- Similarly for x & y string
else if(s1.length() > s2.length())
return 0;
This means that if your x length is more then y length. You will send false. Which means you are sorting them in increasing order of length.
- When length of x and y is equal
else return s1 < s2;
This means that if x is lexographically less then y will send true
a is lexographically shorter then b. lexographical increasing order of alphabets are (a,b,c,…z)
please explain the output for given input
So your output is
a b c xy abc bca
- This is because a is lexographically less then b
b is lexographically less then c - Now xy length is greater then c, so xy comes(because of it’s greater length)
- Since abc & bca have same length so we will see which is lexographically smaller. And you will see abc is lexographically smaller then bca, because of first letter in both the string, we can conclude a is lexographically shorter then b.
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.