adding the list sort example

This commit is contained in:
Jidong Xiao
2024-02-09 13:43:31 -05:00
parent 8c639eb937
commit ed6e8e2116
2 changed files with 60 additions and 0 deletions

View File

@@ -118,6 +118,39 @@ compare function as STL vector.
The value of any associated vector iterator must be re-assigned / re-initialized after these operations.
### List Sort Example
The following [example](list_sort.cpp) demonstrate how to call the list sort function.
```cpp
#include <iostream>
#include <list>
int main() {
// Create a list of integers
std::list<int> numbers = {5, 2, 9, 3, 7};
// Print the original list
std::cout << "Original list: ";
for (int num : numbers) {
std::cout << num << " ";
}
std::cout << std::endl;
// Sort the list in ascending order
numbers.sort();
// Print the sorted list
std::cout << "Sorted list: ";
for (int num : numbers) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
```
## 9.7 Erase & Iterators
STL lists and vectors each have a special member function called erase. In particular, given list of ints s,

View File

@@ -0,0 +1,27 @@
#include <iostream>
#include <list>
int main() {
// Create a list of integers
std::list<int> numbers = {5, 2, 9, 3, 7};
// Print the original list
std::cout << "Original list: ";
for (int num : numbers) {
std::cout << num << " ";
}
std::cout << std::endl;
// Sort the list in ascending order
numbers.sort();
// Print the sorted list
std::cout << "Sorted list: ";
for (int num : numbers) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}