STL algorithms hoạt động trên bất kỳ container nào qua iterator — tách logic khỏi cấu trúc dữ liệu.
cpp
std::vector<int> v = {3, 1, 4, 1, 5, 9};
// sort — O(n log n), sửa tại chỗ
std::sort(v.begin(), v.end());
std::sort(v.begin(), v.end(), std::greater<int>()); // giảm dần
// find — O(n), trả iterator (v.end() nếu không thấy)
auto it = std::find(v.begin(), v.end(), 5);
if (it != v.end()) { /* tìm thấy */ }
// accumulate — fold từ trái sang phải
int sum = std::accumulate(v.begin(), v.end(), 0); // 23
// transform — apply function, ghi vào output range
std::vector<int> doubled;
std::transform(v.begin(), v.end(), std::back_inserter(doubled),
[](int x) { return x * 2; });Lưu ý: dùng std::sort trên std::list sẽ không compile — list không có random access iterator; phải dùng list.sort().
STL algorithms work on any container through iterators — separating logic from data structure.
cpp
std::vector<int> v = {3, 1, 4, 1, 5, 9};
// sort — O(n log n), in-place
std::sort(v.begin(), v.end());
std::sort(v.begin(), v.end(), std::greater<int>()); // descending
// find — O(n), returns iterator (v.end() if not found)
auto it = std::find(v.begin(), v.end(), 5);
if (it != v.end()) { /* found */ }
// accumulate — left fold
int sum = std::accumulate(v.begin(), v.end(), 0); // 23
// transform — apply function, write to output range
std::vector<int> doubled;
std::transform(v.begin(), v.end(), std::back_inserter(doubled),
[](int x) { return x * 2; });Note: std::sort on std::list won't compile — list lacks random access iterators; use list.sort() instead.