-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWithUserDefinedClassesUsingComparator.cpp
More file actions
72 lines (57 loc) · 1.51 KB
/
WithUserDefinedClassesUsingComparator.cpp
File metadata and controls
72 lines (57 loc) · 1.51 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
/**
* \file WithUserDefinedClassesUsingComparator.cpp
* \brief
*
* \todo
*/
#include<iostream>
#include<set>
#include<string>
class Message
{
public:
std::string m_MsgContent;
std::string m_sentBy;
std::string m_recivedBy;
Message(std::string sentBy, std::string recBy, std::string msg) :
m_MsgContent(msg), m_sentBy(sentBy), m_recivedBy(recBy)
{}
friend std::ostream& operator<<(std::ostream& os, const Message& obj);
};
std::ostream& operator<<(std::ostream& os, const Message& obj)
{
os<<obj.m_sentBy<<" :: "<<obj.m_MsgContent<<" :: "<<obj.m_recivedBy<<std::endl;
return os;
}
class MessageUserComparator
{
std::string m_userName;
public:
MessageUserComparator(std::string userName) :
m_userName(userName)
{}
bool operator() (const Message& m1, const Message& m2) const
{
if (m1.m_sentBy < m2.m_sentBy)
return true;
else
return false;
}
};
int main()
{
Message msg1("user_1", "Hello", "user_2");
Message msg2("user_1", "Hello", "user_3");
Message msg3("user_3", "Hello", "user_1");
Message msg4("user_1", "Hello", "user_3");
std::cout << "Set that contains Messages sent by user - user_1" << std::endl;
std::set<Message, MessageUserComparator> setOfMsgs_1(MessageUserComparator("user_1"));
setOfMsgs_1.insert(msg1);
setOfMsgs_1.insert(msg2);
setOfMsgs_1.insert(msg3);
setOfMsgs_1.insert(msg4);
// Iterate through all the elements in a set and display the value.
for (std::set<Message>::iterator it=setOfMsgs_1.begin(); it != setOfMsgs_1.end(); ++it)
std::cout << *it ;
return 0;
}