-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComparison.cpp
More file actions
55 lines (45 loc) · 1.52 KB
/
Comparison.cpp
File metadata and controls
55 lines (45 loc) · 1.52 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
/**
* \file Comparison.cpp
* \brief
*
* \todo
*/
#include <iostream>
#include <map>
#include <string>
#include <iterator>
struct WordGreaterComparator
{
bool operator()(const std::string & left, const std::string & right) const
{
return (left > right);
}
};
int main()
{
std::cout<<"Default sorting criteria for keys i.e. operator < for std::string"<<std::endl;
// Default sorting criteria for keys i.e. operator < for std::string
std::map<std::string, int> mapOfWords;
mapOfWords.insert(std::make_pair("earth", 1));
mapOfWords.insert(std::make_pair("moon", 2));
mapOfWords.insert(std::make_pair("sun", 3));
std::map<std::string, int>::iterator it = mapOfWords.begin();
for( ; it != mapOfWords.end(); it++)
std::cout<<it->first<<" :: "<<it->second<<std::endl;
std::cout<<"External sorting criteria for keys "<<std::endl;
std::map<std::string, int, WordGreaterComparator > mapOfWords_2;
mapOfWords_2.insert(std::make_pair("earth", 1));
mapOfWords_2.insert(std::make_pair("moon", 2));
mapOfWords_2.insert(std::make_pair("sun", 3));
for(std::map<std::string, int>::iterator it = mapOfWords_2.begin(); it != mapOfWords_2.end(); it++)
std::cout<<it->first<<" :: "<<it->second<<std::endl;
std::cout << std::endl;
//Compilation Error : As the sorting criteria is not same
/*
if(mapOfWords == mapOfWords_2)
std::cout<< "Maps are same" <<std::endl;
else
std::cout<< "Maps are not same" << std::endl;
*/
return 0;
}