-
Notifications
You must be signed in to change notification settings - Fork 0
/
case_insenstive_string_map.cpp
38 lines (35 loc) · 1.08 KB
/
case_insenstive_string_map.cpp
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
#include <map>
#include <string>
#include <iostream>
#include <locale>
#include <utility>
class ignore_case_less
{
public:
bool operator()(const std::string& lhs, const std::string& rhs) {
if (lhs.size() < rhs.size()) {
return true;
}
if (lhs.size() == rhs.size()) {
for (int i = 0; i < lhs.size(); ++i) {
if (std::toupper(lhs[i]) == std::toupper(rhs[i])) {
continue;
}
return lhs[i] < rhs[i];
}
return false;
}
return true;
}
};
int main()
{
std::map<std::string, int, ignore_case_less> m;
m.insert(std::make_pair<std::string, int>("jack", 21));
m.insert(std::make_pair<std::string, int>("Jack", 22));
m.insert(std::make_pair<std::string, int>("bob", 22));
m.insert(std::make_pair<std::string, int>("BOB", 25));
for (auto iter = m.begin(); iter != m.end(); ++iter) {
std::cout << iter->first << ": " << iter->second << std::endl;
}
}