← Back to C++ Course | Chapter 13: STL Containers & Algorithms | Lesson 6 of 15

C++ STL Algorithms

STL algorithms are ready-made tools that sort, search, and count items in a collection, like a set of helpers for your list. You do not have to write the loops yourself.
Syntax
cpp
#include <algorithm>

std::sort(container.begin(), container.end());
auto position = std::find(container.begin(), container.end(), value);

Sorting with std::sort

std::sort rearranges the elements of a container into ascending order by default, using an efficient introsort implementation under the hood, and accepts an optional custom comparison function when you need a different ordering.

Example: Sorting with std::sort

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
#include <algorithm>

// Program execution starts in main()
int main() {
	// Create a vector named nums (a resizable array)
	std::vector<int> nums = {5, 2, 8, 1};
	std::sort(nums.begin(), nums.end());
	for (int n : nums) std::cout << n << " ";
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Finding with std::find

std::find searches a range for the first element equal to a given value and returns an iterator pointing to it — or an iterator equal to the range's end if no match exists, which you check for before dereferencing the result.

Example: Finding with std::find

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
#include <algorithm>

// Program execution starts in main()
int main() {
	// Create a vector named nums (a resizable array)
	std::vector<int> nums = {1, 2, 3, 4};
	auto it = std::find(nums.begin(), nums.end(), 3);
	// Print to the console with cout
	std::cout << (it != nums.end()) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Counting with std::count

std::count tells you exactly how many times a specific value appears within a range, while its companion count_if lets you count elements that satisfy a custom predicate instead of matching an exact value.

Example: Counting with std::count

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
#include <algorithm>

// Program execution starts in main()
int main() {
	// Create a vector named nums (a resizable array)
	std::vector<int> nums = {1, 2, 2, 3, 2};
	// Print to the console with cout
	std::cout << std::count(nums.begin(), nums.end(), 2) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Modifying Vectors

Modifying algorithms like std::reverse and std::replace let you transform a container's contents in place — reversing element order, or swapping every occurrence of one value for another — without writing a manual loop yourself.

Example: Modifying Vectors

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
#include <algorithm>

// Program execution starts in main()
int main() {
	// Create a vector named nums (a resizable array)
	std::vector<int> nums = {1, 2, 3};
	std::reverse(nums.begin(), nums.end());
	for (int n : nums) std::cout << n << " ";
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Min and Max Functions

The standard library also provides simple std::min and std::max functions for comparing individual values, alongside std::min_element and std::max_element for finding the smallest or largest element across an entire range.

Example: Min and Max Functions

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
#include <algorithm>

// Program execution starts in main()
int main() {
	// Create a vector named nums (a resizable array)
	std::vector<int> nums = {5, 2, 8, 1};
	auto maxIt = std::max_element(nums.begin(), nums.end());
	// Print to the console with cout
	std::cout << *maxIt << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting #include <algorithm> before using std::sort or std::find.
  2. Calling std::sort on a std::list, which does not compile because it needs random access iterators.
  3. Passing a range where the first iterator comes after the last, which is undefined behavior.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.