The C++ algorithm Library: sort, find, count
Learn the C++ algorithm library, including sort, find, count, and other STL algorithms for efficient data processing.
The C++ <algorithm> library provides powerful and commonly used functions for working with vectors and other STL containers. In this article, we explore three of the most frequently used functions: sort, find, and count, explained with practical examples.
1) sort Function
std::sort sorts a range (such as a vector or array) in ascending order by default. Sorting usually runs with an average time complexity of O(n log n).
Basic Usage
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {30, 10, 40, 20};
sort(v.begin(), v.end());
for (int n : v)
cout << n << " ";
}
Output: 10 20 30 40
Descending Sort
sort(v.begin(), v.end(), greater<int>());
Custom Comparator
sort(v.begin(), v.end(), [](int a, int b) {
return a % 10 < b % 10; // sort by last digit
});
2) find Function
std::find searches for a value within a range and returns an iterator
to the first match.
If the value is not found, it returns the end() iterator.
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {10, 20, 30, 40};
auto it = find(v.begin(), v.end(), 30);
if (it != v.end())
cout << "Found: " << *it;
else
cout << "Not found!";
}
Searching Strings
vector<string> names = {"Alice", "Emma", "John"};
auto it = find(names.begin(), names.end(), "Emma");
3) count Function
std::count counts how many times a value appears within a range.
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {10, 20, 20, 30, 20};
int c = count(v.begin(), v.end(), 20);
cout << "Count of 20: " << c;
}
Output: Count of 20: 3
Conditional Counting (count_if)
int c = count_if(v.begin(), v.end(), [](int x) {
return x % 2 == 0; // count even numbers
});
4) Practical Example — Counting Words
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
using namespace std;
int main() {
vector<string> words = {"cpp", "java", "cpp", "python", "cpp"};
int cppCount = count(words.begin(), words.end(), "cpp");
cout << "\"cpp\" appears " << cppCount << " times";
}
5) Using sort, find, and count Together
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {50, 20, 10, 20, 40};
sort(v.begin(), v.end()); // 10 20 20 40 50
auto it = find(v.begin(), v.end(), 20); // first occurrence of 20
int c = count(v.begin(), v.end(), 20); // count 20s
cout << "First 20 found at index: " << (it - v.begin()) << endl;
cout << "Total count of 20: " << c;
}
6) When to Use Each?
- sort: For sorting any container or range
- find: For locating a specific element quickly
- count: For counting occurrences of a value
The <algorithm> library is one of the most powerful tools in the STL for performing high-performance operations on containers.
7) TL;DR
- sort: O(n log n) sorting function
- find: Searches for a value and returns an iterator
- count: Counts how many times a value occurs
- count_if: Counts elements that meet a condition
- All examples work with Visual Studio 2022 and GCC.
Related Articles
Iterators and Their Use in the C++ STL
Learn iterators in C++ and how they work with STL containers and algorithms to traverse and manipulate data efficiently.
Overview of the C++ Standard Library
Learn the C++ Standard Library, including STL containers, algorithms, iterators, and essential components for modern C++ development.
Unique Elements with std::set and std::multiset in C++
Learn std::set and std::multiset in C++ to manage unique elements, ordered collections, and efficient search operations.
Using std::vector in C++
Learn how to use std::vector in C++ for dynamic arrays, efficient element management, and practical programming examples.