Sorting Localized Strings - C++ Internationalization

C++ examples for Internationalization:Locale

Description

Sorting Localized Strings

Demo Code

#include <iostream>
#include <locale>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

bool localeLessThan (const string& s1, const string& s2) {

   const collate<char>& col = use_facet<collate<char> >(locale()); // Use the global locale

   const char* pb1 = s1.data();
   const char* pb2 = s2.data();

   return (col.compare(pb1, pb1 + s1.size(), pb2, pb2 + s2.size()) < 0);
}

int main() {/* w w w.  j  a  va  2 s .  c om*/
   string s1 = "d";
   string s2 = "d";

   vector<string> v;
   v.push_back(s1);
   v.push_back(s2);

   sort(v.begin(), v.end());

   for (vector<string>::const_iterator p = v.begin();p != v.end(); ++p)
      cout << *p << endl;

   locale::global(locale("german"));
   sort(v.begin(), v.end(), localeLessThan);

   for (vector<string>::const_iterator p = v.begin();p != v.end(); ++p)
      cout << *p << endl;
}

Result


Related Tutorials