Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
collate.cpp
Go to the documentation of this file.
1
29#include <iostream> // for cout
30#include <locale> // for collate
31
32#include <examples.h>
33
34int main ()
35{
36 const std::string s1 ("blue");
37 const std::string s2 ("blues");
38
39 std::cout << "String 1 : " << s1
40 << "\nString 2 : " << s2 << '\n';
41
42 // obtain a reference to the collate facet
43 // installed in the locale imbued in cout
44 const std::collate<char>& co =
45 std::use_facet<std::collate<char> >(std::cout.getloc ());
46
47 // use the facet to compare the two strings
48 int res = co.compare (s1.c_str (), s1.c_str () + s1.length (),
49 s2.c_str (), s2.c_str () + s2.length () - 1);
50
51 // strings should collate equal
52 std::cout << "Strings \""
53 << s1.substr(0, s1.length ()) << "\" and \""
54 << s2.substr(0, s2.length () - 1)
55 << "\" should compare equal : "
56 << (res ? "not " : "") << "equal\n";
57
58 res = co.compare (s1.c_str (), s1.c_str () + s1.length (),
59 s2.c_str (), s2.c_str () + s2.length ());
60
61 // strings should collate unequal
62 std::cout << "Strings \""
63 << s1.c_str () << "\" and \"" << s2.c_str ()
64 << "\" should compare NOT equal : "
65 << (res ? "not " : "") << "equal\n";
66
67 // compute hash values for the two strings
68 // expected values:
69 // ILP32: 431029 and 6896579
70 // LP64: 1651275109 and 422726428019
71 const long hash1 = co.hash (s1.c_str (), s1.c_str () + s1.length ());
72 const long hash2 = co.hash (s2.c_str (), s2.c_str () + s2.length ());
73
74 // normalize the two hash values for portability
75 // between 32-bit and 64-bit longs
76 const long norm1 = 440460L;
77 const long norm2 = 7898347L;
78
79 // expect 431029 on output
80 std::cout << "Normalized hash value for \"" << s1.c_str () << "\" : "
81 << hash1 % norm1
82 << "\nNormalized hash value for \"" << s2.c_str () << "\" : "
83 << hash2 % norm2
84 << '\n';
85
86 return 0;
87}
#define s1
Definition 17.names.cpp:242
#define s2
Definition 17.names.cpp:268
#define res
Definition 17.names.cpp:531
int main()
Definition collate.cpp:34