-
Notifications
You must be signed in to change notification settings - Fork 0
/
maps and operator overloading.cpp
74 lines (58 loc) · 1.36 KB
/
maps and operator overloading.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
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
// Last sanity check: 2020-05-18
//
// main.cpp
// Exer
//
// Created by Alex Seceleanu on 4/7/18.
// Copyright © 2018 Alex Seceleanu. All rights reserved.
#include <stdio.h>
#include<iostream>
#include<vector>
#include<map>
using namespace std;
class Person
{
private:
string name;
int age;
public:
Person(): name ("name"), age (0) { }
Person (string name, int age) :
name (name), age (age) { }
Person(const Person &other)
{
// cout << "Copy constructor running!"<<endl;
name = other.name;
age = other.age;
}
void print() const
{
cout << name << "\t:\t" << age << flush;
}
//Operator overloading!
bool operator< (const Person &other) const
{
if ( name == other.name)
return age < other.age;
else
return name < other.name;
}
};
int main();
{
map <Person, int> people;
people[Person("Mih", 18)] = 18;
people[Person("Mih", 188)] = 44;
people[Person("Alx", 34)] = 34;
people[Person("Vld", 52)] = 52;
cout << endl;
// cout << "K|Name\t\tAge"<<endl;
for ( map<Person, int>::iterator it = people.begin(); it != people.end(); it++ )
{
cout << it -> second << " : " << flush;
it -> first.print();
cout << endl;
}
cout << endl;
return 0;
}