OnlineCBT Logo OnlineCBT
C++ · LESSON 17

STL Algorithms: sort, find, count, and Iterators

By Ratan Updated: 15 Oct 2026

Apply high-performance algorithms like std::sort(), std::find(), and iterator navigation.

Level: Beginner to Advanced
Duration: ~60 Mins Deep-Dive

Use algorithms over half-open ranges

Many standard algorithms take a begin iterator and an end iterator. The range includes begin and excludes end. end is a boundary, not an element to dereference. Algorithms separate the operation from the container's storage representation.

Sort, find and count

#include 
#include 
#include 
int main() {
    std::vector marks{70, 40, 90, 70};
    std::sort(marks.begin(), marks.end());
    for (int mark : marks) std::cout << mark << ' ';
    std::cout << '\n';
    auto found = std::find(marks.begin(), marks.end(), 70);
    if (found != marks.end()) std::cout << "Found " << *found << '\n';
    std::cout << "Seventies: "
              << std::count(marks.begin(), marks.end(), 70) << '\n';
}

Expected sorted values are 40, 70, 70, 90. find locates the first 70, and count reports 2. find is linear; sorting does not automatically make a later std::find call logarithmic.

Iterator capabilities matter

std::sort requires random-access iterators, which vector provides. std::list does not meet that requirement; use its member sort. Simpler algorithms such as find can work with weaker iterator categories. An algorithm's requirements are part of its interface, not an implementation detail you can ignore.

Custom comparisons

std::sort(marks.begin(), marks.end(), [](int a, int b) {
    return a > b;
});

This sorts descending. Use a strict comparator: a >= b is incorrect because equal values would compare as preceding themselves. For records, compare the field that defines the requested order and decide how ties are handled.

Erasing is a separate operation

In C++17, remove_if rearranges retained elements and returns a new logical end; it does not shrink a vector by itself. Complete the erase-remove pattern:

marks.erase(std::remove_if(marks.begin(), marks.end(),
    [](int mark) { return mark < 50; }), marks.end());

After this operation the sample's 40 is gone. The vector's size is now three. Iterators affected by erasure must not be reused.

Practice

  1. Use count_if to count marks greater than or equal to 70; expect 3.
  2. Search for 99 and verify the result equals end before dereferencing.
  3. Sort student records by marks, then by roll number for deterministic ties.
  4. Explain why a half-open empty range has begin equal to end and still works naturally with these algorithms.

📝 Live Lesson Practice

HTML/CSS JavaScript Python C++ C PHP
⌨️ Practice Inputs (लाइव इनपुट भरें) (खाली होने पर RED, भरने पर GREEN underline)
💻 Code Editor (Monaco VS Code Engine)
👀 Live Preview
Address Contact

+91 7877547686

E-mail

onlinecbtportal@gmail.com

Helpline Number

+91 7877547686


Click To Download
Get it on Google Play

ऐप डाउनलोड करने
के लिए Google Play पर
उपलब्ध है

WhatsApp Chat