C++ Algorithms
The <algorithm> header provides dozens of generic algorithms that work on any container through iterators. sort() sorts a range, find() searches for a value (returns an iterator), and binary_search() checks if a value exists in a sorted range. These algorithms are efficient and eliminate boilerplate code.
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> v = {4, 1, 7, 3, 9};
std::sort(v.begin(), v.end());
auto it = std::find(v.begin(), v.end(), 7);
bool found = std::binary_search(v.begin(), v.end(), 3);
std::cout << "Found 7? " << (it != v.end()) << "\n";
std::cout << "Binary found 3? " << found;
return 0;
}
Try it Yourself →