📌 What are STL Algorithms in C++?
The STL Algorithms library (defined in the and headers) provides over 100 high-performance, template-based functions that operate on ranges of elements defined by iterators. Rather than writing manual raw loops for sorting, searching, filtering, or transforming data, developers rely on these standardized algorithms. Because they are decoupled from container implementations via iterators, a single algorithm like std::sort works identically on vectors, raw arrays, or deques while achieving optimal hardware-level performance.
#include
#include
#include
int main() {
std::vector sensorReadings = {45, 12, 78, 12, 90, 33};
// 1. std::find: Locate first occurrence of value 78
auto it = std::find(sensorReadings.begin(), sensorReadings.end(), 78);
if (it != sensorReadings.end()) {
std::cout << "Found value 78 at index: " << std::distance(sensorReadings.begin(), it) << "\n";
}
// 2. std::count: Count occurrences of value 12
int count12 = std::count(sensorReadings.begin(), sensorReadings.end(), 12);
std::cout << "Value 12 appears " << count12 << " time(s).\n";
// 3. std::all_of: Check if all readings are positive (> 0)
bool allPositive = std::all_of(sensorReadings.begin(), sensorReadings.end(), [](int n) {
return n > 0;
});
std::cout << "Are all sensor readings positive? " << (allPositive ? "YES" : "NO") << "\n";
return 0;
}
std::sort executes IntroSort (guaranteeing $O(N \log N)$ worst-case performance), while rearranging routines reorder elements in-place.
#include
#include
#include
int main() {
std::vector scores = {42, 15, 99, 88, 23};
// 1. std::sort: Ascending order
std::sort(scores.begin(), scores.end());
std::cout << "Sorted Ascending: ";
for (int s : scores) std::cout << s << " ";
std::cout << "\n";
// 2. Custom sorting using lambda (Descending order)
std::sort(scores.begin(), scores.end(), [](int a, int b) {
return a > b;
});
std::cout << "Sorted Descending: ";
for (int s : scores) std::cout << s << " ";
std::cout << "\n";
// 3. std::reverse: Reverse element order
std::reverse(scores.begin(), scores.end());
std::cout << "Reversed Vector: ";
for (int s : scores) std::cout << s << " ";
std::cout << "\n";
return 0;
}
) compute reductions, inner products, and prefix sums.
#include
#include
#include
#include
int main() {
std::vector baseValues = {1, 2, 3, 4, 5};
std::vector squaredValues(baseValues.size());
// 1. std::transform: Square each element
std::transform(baseValues.begin(), baseValues.end(), squaredValues.begin(), [](int x) {
return x * x;
});
std::cout << "Squared Values: ";
for (int val : squaredValues) std::cout << val << " ";
std::cout << "\n";
// 2. std::accumulate: Compute sum of squared values
int totalSum = std::accumulate(squaredValues.begin(), squaredValues.end(), 0);
std::cout << "Total Sum of Squares: " << totalSum << "\n";
return 0;
}
#include
#include
#include
#include
int main() {
std::cout << "============================================\n";
std::cout << "💼 ENTERPRISE PAYROLL PROCESSING PIPELINE\n";
std::cout << "============================================\n";
std::vector salaries = {55000.0, 120000.0, 75000.0, 45000.0, 95000.0};
// 1. Sort salaries descending to find highest earners
std::sort(salaries.begin(), salaries.end(), std::greater());
std::cout << "Salaries (Highest to Lowest): ";
for (double s : salaries) std::cout << "$" << s << " ";
std::cout << "\n";
// 2. Apply a 10% performance bonus inflation to all salaries
std::transform(salaries.begin(), salaries.end(), salaries.begin(), [](double salary) {
return salary * 1.10;
});
// 3. Compute total payroll expenditure using std::accumulate
double totalPayroll = std::accumulate(salaries.begin(), salaries.end(), 0.0);
double averageSalary = totalPayroll / salaries.size();
std::cout << "Total Payroll After 10% Bonus: $" << totalPayroll << "\n";
std::cout << "Average Inflated Salary: $" << averageSalary << "\n";
std::cout << "============================================\n";
return 0;
}
C++ STL ALGORITHMS REFERENCE MATRIX:
┌─────────────────────┬───────────────────────────┬───────────────────────────┬─────────────────────────────────┐
│ Algorithm Category │ Header │ Common Functions │ Time Complexity │
├─────────────────────┼───────────────────────────┼───────────────────────────┼─────────────────────────────────┤
│ Non-Modifying │ `` │ `find`, `count`, `all_of` │ O(N) │
│ Sorting & Ordering │ `` │ `sort`, `binary_search` │ O(N log N) │
│ Modifying/Mutating │ `` │ `reverse`, `transform` │ O(N) │
│ Numeric Reductions │ `` │ `accumulate`, `inner_product`│ O(N) │
├─────────────────────┴───────────────────────────┴───────────────────────────┴─────────────────────────────────┤
│ ENGINEERING BEST PRACTICES: │
│ 1. Always prefer standard STL algorithms over manual raw `for` loops; they express intent clearly and are optimized.│
│ 2. Leverage lambda expressions inline with algorithms to eliminate redundant helper function declarations. │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────┘