C++ STL std::max()

What ‘datatypes’ can be compared using the max() function?
Can I compare two numeric strings using max()? What should be the result of the following lines of code?

string s= “1111119”;
string t= “1111111”;
string m= max(s, t);
cout<<"maximum: "<<m<<endl;

RTFM: std::max - cppreference.com

1 Like

I’m sorry but I can’t find the answer on the site you have mentioned. Could you please share the your answer? Meaning no references. Pure explanation.

Which datatypes can be used in std::max? Anything that has operator< defined. What does it do? That depends on what the operator< for that type does.

So you’se using std::max without a custom comparator function, which means it uses operator< to do the comparison. If you go to std::string on cppreference, there is an explanation what operator< for strings does: lexicographical comparison. So text compare.

This information is indeed in cppreference (top site, by the way), but it can take a bit of puzzling to extract this info.

1 Like