Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is max_element not showing the largest string in vector C++?

In the code below I attempt to print the largest std::string in a std::vector using std::max_element.

I expected the output of the code below to be:

Harmlessness

The actual output I got is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This

The code:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main(){
    vector <string> strlist;
    strlist.push_back("This");
    strlist.push_back("Harmless");
    strlist.push_back("Harmlessness");
    cout << *max_element(strlist.begin(), strlist.end());
    return 0;
}

My question:
Can you explain why the code produced the actual output above and not the one I expected ?

>Solution :

The default comparator for std::string does an alphabetical comparisson.
"This" comes later in this order than any string starting with "H".

You can use another overload of std::max_element that accepts an explicit comparator argument:

template<class ForwardIt, class Compare> constexpr
ForwardIt max_element( ForwardIt first, ForwardIt last, Compare comp);

If you want to compare strings by length, you can use:

#include <iostream>
#include <algorithm>
#include <vector>

int main() {
    std::vector <std::string> strlist;
    strlist.push_back("This");
    strlist.push_back("Harmless");
    strlist.push_back("Harmlessness");
    
    // Use an explicit comparator, in this case with a lambda:
    std::cout << *max_element(strlist.begin(), strlist.end(), 
                         [](std::string const& a, std::string const& b) {return a.length() < b.length(); });
    return 0;
}

Output:

Harmlessness

Side note: better to avoid using namespace std – see here Why is "using namespace std;" considered bad practice?.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading