Welcome to The Coding College! This tutorial explores the C++ <algorithm>
library, a powerful collection of functions for performing common operations on containers such as arrays, vectors, lists, and other Standard Template Library (STL) data structures.
What is the <algorithm>
Library?
The <algorithm>
library provides functions for:
- Sorting: Arrange elements in ascending/descending order.
- Searching: Find specific elements or ranges.
- Modifying: Transform or reorder elements.
- Utility Operations: Perform computations like minimum, maximum, and comparisons.
To use these functions, include the library in your program:
#include <algorithm>
Key Features of <algorithm>
- Versatile: Works seamlessly with STL containers.
- Generic: Uses iterators, making it compatible with various data structures.
- Performance Optimized: Provides efficient implementations of common algorithms.
Commonly Used <algorithm>
Functions
1. Sorting Functions
Function | Description |
---|---|
sort() | Sorts a range in ascending order. |
stable_sort() | Maintains the relative order of equal elements. |
partial_sort() | Sorts the first few elements. |
2. Searching Functions
Function | Description |
---|---|
binary_search() | Checks if an element exists (sorted containers). |
find() | Finds an element in a range. |
3. Utility Functions
Function | Description |
---|---|
min() | Returns the smaller of two elements. |
max() | Returns the larger of two elements. |
swap() | Swaps the values of two elements. |
4. Modification Functions
Function | Description |
---|---|
reverse() | Reverses the order of elements in a range. |
transform() | Applies a function to each element. |
remove() | Removes specific values from a range. |
Example: Sorting a Vector
Here’s how to sort a vector of integers using the <algorithm>
library:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> numbers = {50, 20, 40, 10, 30};
// Sort in ascending order
sort(numbers.begin(), numbers.end());
cout << "Sorted numbers: ";
for (int num : numbers) {
cout << num << " ";
}
return 0;
}
Output:
Sorted numbers: 10 20 30 40 50
Example: Binary Search
Check if an element exists in a sorted range:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> numbers = {10, 20, 30, 40, 50};
// Check for existence of an element
bool found = binary_search(numbers.begin(), numbers.end(), 30);
if (found) {
cout << "30 is in the list." << endl;
} else {
cout << "30 is not in the list." << endl;
}
return 0;
}
Output:
30 is in the list.
Example: Reverse a Range
Reverse the order of elements in a container:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> numbers = {10, 20, 30, 40, 50};
// Reverse the vector
reverse(numbers.begin(), numbers.end());
cout << "Reversed numbers: ";
for (int num : numbers) {
cout << num << " ";
}
return 0;
}
Output:
Reversed numbers: 50 40 30 20 10
Example: Find the Maximum and Minimum
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int a = 10, b = 20;
cout << "Maximum: " << max(a, b) << endl;
cout << "Minimum: " << min(a, b) << endl;
return 0;
}
Output:
Maximum: 20
Minimum: 10
Using Custom Comparators
You can pass custom comparison functions to certain algorithms.
Example: Sort in Descending Order
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool descending(int a, int b) {
return a > b;
}
int main() {
vector<int> numbers = {10, 20, 30, 40, 50};
// Sort in descending order
sort(numbers.begin(), numbers.end(), descending);
cout << "Descending order: ";
for (int num : numbers) {
cout << num << " ";
}
return 0;
}
Output:
Descending order: 50 40 30 20 10
Real-Life Example: Removing Duplicates
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> numbers = {10, 20, 10, 30, 20, 40, 50};
// Remove duplicates
sort(numbers.begin(), numbers.end());
auto it = unique(numbers.begin(), numbers.end());
numbers.erase(it, numbers.end());
cout << "Unique elements: ";
for (int num : numbers) {
cout << num << " ";
}
return 0;
}
Output:
Unique elements: 10 20 30 40 50
Summary
The <algorithm>
library is a cornerstone of modern C++ programming, providing efficient and reusable implementations of common operations. Whether you need to sort, search, modify, or transform data, this library has you covered.
For more tutorials, visit The Coding College, your go-to resource for mastering C++! 🚀