The standard has std::locale for std::locale -specific things like sorting (sorting). If the environment contains LC_COLLATE=en_US.utf8 or similar, this program will sort the lines as desired.
#include <algorithm> #include <functional> #include <iostream> #include <iterator> #include <locale> #include <string> #include <vector> class collate_in : public std::binary_function<std::string, std::string, bool> { protected: const std::collate<char> &coll; public: collate_in(std::locale loc) : coll(std::use_facet<std::collate<char> >(loc)) {} bool operator()(const std::string &a, const std::string &b) const { // std::collate::compare() takes C-style string (begin, end)s and // returns values like strcmp or strcoll. Compare to 0 for results // expected for a less<>-style comparator. return coll.compare(a.c_str(), a.c_str() + a.size(), b.c_str(), b.c_str() + b.size()) < 0; } }; int main() { std::vector<std::string> v; copy(std::istream_iterator<std::string>(std::cin), std::istream_iterator<std::string>(), back_inserter(v)); // std::locale("") is the locale from the environment. One could also // std::locale::global(std::locale("")) to set up this program global // first, and then use locale() to get the global locale, or choose a // specific locale instead of using the environment's. sort(v.begin(), v.end(), collate_in(std::locale(""))); copy(v.begin(), v.end(), std::ostream_iterator<std::string>(std::cout, "\n")); return 0; }
$ cat> file
f
รฉ
e
d
^ D
$ LC_COLLATE = C ./a.out file
d
e
f
รฉ
$ LC_COLLATE = en_US.utf8 ./a.out file
d
e
รฉ
f
I was told that std::locale::operator()(a, b) exists, breaking the wrapper std::collate<>::compare(a, b) < 0 that I wrote above.
#include <algorithm> #include <iostream> #include <iterator> #include <locale> #include <string> #include <vector> int main() { std::vector<std::string> v; copy(std::istream_iterator<std::string>(std::cin), std::istream_iterator<std::string>(), back_inserter(v)); sort(v.begin(), v.end(), std::locale("")); copy(v.begin(), v.end(), std::ostream_iterator<std::string>(std::cout, "\n")); return 0; }
source share