(C++) std::lexicographical\_compare
January 7, 2018 · View on GitHub
(C++) std::lexicographical_compare
std::lexicographical_compare is an STL algorithm to determine if two sequences of characters are in an alphabetically sorted order. Note that std::strings are compared this way by default.
#include <algorithm> #include <cassert> #include <string> int main() { const std::string s1 = "Bilderbikkel"; const std::string s2 = "Parachutemeisje"; //Assume Bilderbikkel comes before Parachutemeisje //with std::lexicographical_compare assert(std::lexicographical_compare( s1.begin(),s1.end(),s2.begin(),s2.end()) == true); //Assume Bilderbikkel comes before Parachutemeisje //with default std::string behavior assert(s1 < s2); }