codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

83 / 87 topics
76<iostream> Reference77<fstream> Reference78<cmath> Reference79<string> Reference80<cstring> Reference81<ctime> Reference82<vector> Reference83<algorithm> Reference
Tutorials/C++ Programming/Standard Library References
⚡C++ Programming

Standard Library References

Updated 2026-05-12
30 min read

Standard Library References

In this tutorial, we will explore the powerful algorithms provided by the C++ Standard Library. These algorithms are essential for performing common operations on collections of data efficiently and concisely. Whether you're sorting elements, searching for specific values, or merging sorted sequences, understanding these algorithms will greatly enhance your C++ programming skills.

Introduction

The C++ Standard Library includes a rich set of algorithms that operate on ranges defined by iterators. These algorithms are highly optimized and provide a consistent interface, making it easier to write efficient and readable code. In this section, we will cover some of the most commonly used algorithms, including sort, stable_sort, find, count, binary_search, lower_bound, upper_bound, merge, and unique.

Core Content

12.1 Sorting Algorithms

sort

The sort function sorts elements in a range into ascending order using an operator<.

Example:

C++
1#include <iostream>
2#include <vector>
3#include <algorithm>
4
5int main() {
6 std::vector<int> vec = {5, 3, 8, 1, 2};
7 std::sort(vec.begin(), vec.end());
8
9 for (int num : vec) {
10 std::cout << num << " ";
11 }
12 return 0;
13}
Output

12.2 Searching Algorithms

find

The find function searches for the first occurrence of a value in a range.

Example:

C++
1#include <iostream>
2#include <vector>
3#include <algorithm>
4
5int main() {
6 std::vector<int> vec = {5, 3, 8, 1, 2};
7 auto it = std::find(vec.begin(), vec.end(), 3);
8
9 if (it != vec.end()) {
10 std::cout << "Found: " << *it << std::endl;
11 } else {
12 std::cout << "Not found" << std::endl;
13 }
14 return 0;
15}
Output

12.3 Binary Search

binary_search

The binary_search function checks if a value exists in a sorted range.

Example:

C++
1#include <iostream>
2#include <vector>
3#include <algorithm>
4
5int main() {
6 std::vector<int> vec = {1, 2, 3, 5, 8};
7 bool found = std::binary_search(vec.begin(), vec.end(), 5);
8
9 if (found) {
10 std::cout << "Found" << std::endl;
11 } else {
12 std::cout << "Not found" << std::endl;
13 }
14 return 0;
15}
Output

upper_bound

The upper_bound function returns an iterator pointing to the first element greater than a given value in a sorted range.

Example:

C++
1#include <iostream>
2#include <vector>
3#include <algorithm>
4
5int main() {
6 std::vector<int> vec = {1, 2, 3, 5, 8};
7 auto it = std::upper_bound(vec.begin(), vec.end(), 4);
8
9 if (it != vec.end()) {
10 std::cout << "Upper bound: " << *it << std::endl;
11 } else {
12 std::cout << "Not found" << std::endl;
13 }
14 return 0;
15}
Output

unique

The unique function removes consecutive duplicate elements from a range.

Example:

C++
1#include <iostream>
2#include <vector>
3#include <algorithm>
4
5int main() {
6 std::vector<int> vec = {1, 2, 2, 3, 4, 4, 5};
7 auto new_end = std::unique(vec.begin(), vec.end());
8
9 vec.erase(new_end, vec.end());
10
11 for (int num : vec) {
12 std::cout << num << " ";
13 }
14 return 0;
15}
Output

Summary

AlgorithmDescription
sortSorts elements in a range into ascending order.
stable_sortSorts elements while maintaining relative order of equivalent elements.
findSearches for the first occurrence of a value in a range.
countCounts the number of occurrences of a value in a range.
binary_searchChecks if a value exists in a sorted range.
lower_boundReturns an iterator to the first element not less than a given value.
upper_boundReturns an iterator to the first element greater than a given value.
mergeMerges two sorted ranges into a single sorted range.
uniqueRemoves consecutive duplicate elements from a range.

What's Next?

Now that you have a solid understanding of the C++ Standard Library algorithms, it's time to apply your knowledge in real-world projects and programming examples. In the next section, we will explore various projects that will help you practice and reinforce what you've learned.

Stay tuned for more tutorials on advanced topics and practical programming exercises!


Previous<vector> ReferenceNext Projects & Programming Examples

Recommended Gear

<vector> ReferenceProjects & Programming Examples