c++ - Sort by lexicographical_compare() function -
is there way sort string using lexicographical_compare()
function in c++?
i can stl sort, question lexicographical_compare()
function.
you don't need std::lexicographical_compare
sort string. need std::sort
algorithm:
#include <iostream> #include <string> #include <algorithm> int main() { std::string s("qwertyuioplkjhgfdsazxcvbnm"); std::cout << s << "\n"; std::sort(s.begin(), s.end()); std::cout << s << "\n"; }
the same applies sorting collection of strings:
#include <iostream> #include <string> #include <vector> #include <algorithm> int main() { std::vector<std::string> v{"apple" , "apple" ,"apple" , "apple"}; (const auto& s : v) std::cout << s << " "; std::cout << "\n"; std::sort(v.begin(), v.end()); (const auto& s : v) std::cout << s << " "; std::cout << "\n"; }
Comments
Post a Comment