-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmapcmp.cpp
More file actions
73 lines (61 loc) · 1.54 KB
/
mapcmp.cpp
File metadata and controls
73 lines (61 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <iomanip>
#include <map>
#include <string>
#include <sequtils.h>
#include <iterator>
using namespace std;
class RuntimeStringCmp
{
public :
enum cmp_mode { normal, nocase };
private :
const cmp_mode mode;
static bool nocase_compare(char c1, char c2) {
return toupper(c1) < toupper(c2);
}
public:
RuntimeStringCmp(cmp_mode m=normal): mode(m) { }
bool operator() (const string& s1, const string& s2) {
if (mode == normal) {
return s1 < s2;
} else {
return lexicographical_compare(s1.begin(), s1.end(),
s2.begin(), s2.end(),
nocase_compare);
}
}
};
typedef map<string, string, RuntimeStringCmp> StringStringMap;
void fillAndPrint(StringStringMap& col);
int main(int argc, char *argv[])
{
StringStringMap col;
fillAndPrint(col);
RuntimeStringCmp comp(RuntimeStringCmp::nocase);
StringStringMap col2(comp);
fillAndPrint(col2);
return 0;
}
void fillAndPrint(StringStringMap& coll)
{
//fill insert elements in random order
coll["Deutschland"] = "Germany";
coll["deutsch"] = "German";
coll["Haken"] = "snag";
coll["arbeiten"] = "work";
coll["Hund"] = "dog";
coll["gehen"] = "go";
coll["Unternehmen"] = "enterprise";
coll["unternehmen"] = "undertake";
coll["gehen"] = "walk";
coll["Bestatter"] = "undertaker";
//print elements
StringStringMap::iterator pos;
cout.setf(ios::left, ios::adjustfield);
for (pos=coll.begin(); pos!=coll.end(); ++pos) {
cout << setw(15) << pos->first.c_str() << " "
<< pos->second << endl;
}
cout << endl;
}