Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Lesson 33: Algorithms (sort, find, trans | C++ Course lesson | BraveProgrammer
  1. Courses
  2. /
  3. C++ Course

Lesson 33 of 45

·C++

Lesson 33: Algorithms (sort, find, transform, etc.)

13 min

Course progress73%

What you'll learn

Keep these ideas in mind as you work through the lesson.

4 points
  • Sorting, searching, modifying, and numeric algorithms
  • Using lambda expressions as predicates
  • Complexity of common algorithms
  • In‑place vs. out‑of‑place transformations

Algorithms (sort, find, transform, etc.)

The <algorithm> header provides a wealth of generic algorithms that operate on iterator ranges. They are designed to work with any container that supplies the appropriate iterator category.


Commonly used algorithms

  • Sorting – std::sort, std::stable_sort, std::partial_sort.
  • Searching – std::find, std::binary_search, std::lower_bound, std::upper_bound.
  • Modification – std::transform, std::replace, std::remove, std::unique.
  • Numeric – std::accumulate, std::inner_product, std::reduce (C++17).

Example: Sorting a vector of structs by a member field

struct Person {
    std::string name;
    int age;
};
std::vector<Person> people = { {\"Bob\",30}, {\"Alice\",25}, {\"Eve\",35} };
// Sort by age ascending using a lambda comparator
std::sort(people.begin(), people.end(), [](const Person& a, const Person& b) {
    return a.age < b.age;
});
for (const auto& p : people) {
    std::cout << p.name << \" (\" << p.age << \")\
\";
}

Tip: When possible, use the range‑based overloads from <algorithm> (C++20) like std::ranges::sort for clearer syntax.


<Alert type="info">Many algorithms invalidate iterators after they modify the container (e.g., std::remove does not actually erase elements). Pair them with vector::erase when needed.

Make it stickPractice what you've learned1 quiz · 2 practical tests · Optional activities from C++ CourseBrowse activities
Previous lessonLesson 32: Map and Unordered MapContinue to lesson 34Lesson 34: Lambda ExpressionsBack to C++ Course curriculum