fork download
  1. #include <locale>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. /// collate<char> colc;
  8.  
  9.  
  10. template<typename C>
  11. class My_collate : public collate<C>
  12. {
  13. public:
  14. explicit My_collate(size_t r = 0) :
  15. collate<C> {r}
  16. {
  17. }
  18. };
  19.  
  20. My_collate<char> mcolc;
  21.  
  22.  
  23. void print(const string& s1, const string& s2,
  24. const int& rslt)
  25. {
  26. string srslt {};
  27.  
  28. switch(rslt)
  29. {
  30. case 0:
  31. srslt = "equal";
  32. break;
  33.  
  34. case 1:
  35. srslt = "s1 > s2";
  36. break;
  37.  
  38.  
  39. case -1:
  40. srslt = "s1 < s2";
  41. break;
  42. }
  43.  
  44. cout << "comparison of " << s1 << " and " << s2
  45. << " using the mcolc facet : "
  46. << srslt << endl;
  47. }
  48.  
  49.  
  50. void test(const string& s1, const string& s2)
  51. {
  52. /// since compare() operates on char[]s
  53. const char* s1b = s1.data(); /// start of data
  54. const char* s1e = s1b + s1.size(); /// end of data
  55. const char* s2b = s2.data(); /// start of data
  56. const char* s2e = s2b + s2.size(); /// end of data
  57.  
  58. int rslt = mcolc.compare(s1b, s1e, s2b, s2e);
  59.  
  60. /// display results
  61. print(s1, s2, rslt);
  62. }
  63.  
  64.  
  65. int main()
  66. {
  67. test("Hello", "Hello");
  68. test("Hello", "hello");
  69. test("hello", "Hello");
  70. }
  71.  
Success #stdin #stdout 0s 4532KB
stdin
Standard input is empty
stdout
comparison of Hello and Hello using the mcolc facet : equal
comparison of Hello and hello using the mcolc facet : s1 < s2
comparison of hello and Hello using the mcolc facet : s1 > s2