adding overloading operator example
This commit is contained in:
@@ -202,6 +202,59 @@ int main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### List Sort Example - Sorting Class Objects, via Overloading the Less Than Operator
|
||||||
|
|
||||||
|
The following [example](list_sort_objects2.cpp) also demonstrates how to call the list sort function to sort a list which contains class objects.
|
||||||
|
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
#include <iostream>
|
||||||
|
#include <list>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
// Define a simple class representing a person
|
||||||
|
class Person {
|
||||||
|
public:
|
||||||
|
std::string name;
|
||||||
|
int age;
|
||||||
|
|
||||||
|
// Constructor
|
||||||
|
Person(std::string name, int age) : name(name), age(age) {}
|
||||||
|
|
||||||
|
// Overload the < operator for sorting
|
||||||
|
bool operator<(const Person& other) const {
|
||||||
|
// Compare based on age
|
||||||
|
return age < other.age;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
// Create a list of Person objects
|
||||||
|
std::list<Person> people = {
|
||||||
|
{"Alice", 25},
|
||||||
|
{"Bob", 30},
|
||||||
|
{"Charlie", 20}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Print the original list
|
||||||
|
std::cout << "Original list:" << std::endl;
|
||||||
|
for (const auto& person : people) {
|
||||||
|
std::cout << person.name << " (" << person.age << ")" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort the list of Person objects
|
||||||
|
people.sort();
|
||||||
|
|
||||||
|
// Print the sorted list
|
||||||
|
std::cout << "\nSorted list:" << std::endl;
|
||||||
|
for (const auto& person : people) {
|
||||||
|
std::cout << person.name << " (" << person.age << ")" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## 9.7 Erase & Iterators
|
## 9.7 Erase & Iterators
|
||||||
|
|
||||||
STL lists and vectors each have a special member function called erase. In particular, given list of ints s,
|
STL lists and vectors each have a special member function called erase. In particular, given list of ints s,
|
||||||
|
|||||||
45
lectures/09_iterators_linked_lists/list_sort_objects2.cpp
Normal file
45
lectures/09_iterators_linked_lists/list_sort_objects2.cpp
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <list>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
// Define a simple class representing a person
|
||||||
|
class Person {
|
||||||
|
public:
|
||||||
|
std::string name;
|
||||||
|
int age;
|
||||||
|
|
||||||
|
// Constructor
|
||||||
|
Person(std::string name, int age) : name(name), age(age) {}
|
||||||
|
|
||||||
|
// Overload the < operator for sorting
|
||||||
|
bool operator<(const Person& other) const {
|
||||||
|
// Compare based on age
|
||||||
|
return age < other.age;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
// Create a list of Person objects
|
||||||
|
std::list<Person> people = {
|
||||||
|
{"Alice", 25},
|
||||||
|
{"Bob", 30},
|
||||||
|
{"Charlie", 20}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Print the original list
|
||||||
|
std::cout << "Original list:" << std::endl;
|
||||||
|
for (const auto& person : people) {
|
||||||
|
std::cout << person.name << " (" << person.age << ")" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort the list of Person objects
|
||||||
|
people.sort();
|
||||||
|
|
||||||
|
// Print the sorted list
|
||||||
|
std::cout << "\nSorted list:" << std::endl;
|
||||||
|
for (const auto& person : people) {
|
||||||
|
std::cout << person.name << " (" << person.age << ")" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user