-
Notifications
You must be signed in to change notification settings - Fork 2
/
car.cpp
53 lines (45 loc) · 1.09 KB
/
car.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
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
class Car{
private:
string m_manufacturer;
string m_model;
public:
Car(string manufacturer, string model):
m_manufacturer{manufacturer}, m_model{model}{};
friend bool operator<(const Car&, const Car&);
friend ostream& operator<<(ostream&, const Car&);
};
bool operator<(const Car& car1, const Car& car2){
if(car1.m_manufacturer < car2.m_manufacturer){
return true;
}
else if(car1.m_manufacturer > car2.m_manufacturer){
return false;
}
else if(car1.m_model < car2.m_model){
return true;
}
else{
return false;
}
}
ostream& operator<<(ostream& out, const Car& car){
out << "Car(" << car.m_manufacturer << ", " << car.m_model << ")";
return out;
}
int main(int argc, char const *argv[]){
vector<Car> cars;
cars.push_back(Car("Toyota", "Corolla"));
cars.push_back(Car("Honda", "Accord"));
cars.push_back(Car("Toyota", "Camry"));
cars.push_back(Car("Honda", "Civic"));
std::sort(cars.begin(), cars.end());
for(auto &car : cars){
cout << car << endl;
}
return 0;
}