CodeBlanks Logo Icon CodeBlanks

Summary - STL algorithms

Combine sort, find and accumulate to write a sales report.

Summary - STL algorithms

In this group you learned std::sort, std::find, std::count, std::transform and std::accumulate. Now we combine them in a sales report.

On a sales vector we will first sort with std::sort, then search for a specific sale with std::find. In the second step we compute the total sales with std::accumulate. This shows how STL algorithms work together in a real scenario.

Practice Sort sales and search

Sort sales = {150, 80, 200, 90} in ascending order with std::sort. Then search for 150 with std::find and print "found" if present. Do not print the sorted list; print only the search result.

Starter code

main.cpp
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> sales = {150, 80, 200, 90};
    // Sort with std::sort, search 150 with std::find, print result
    return 0;
}

Sample solution

main.cpp
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> sales = {150, 80, 200, 90};
    std::sort(sales.begin(), sales.end());
    auto it = std::find(sales.begin(), sales.end(), 150);
    if (it != sales.end()) {
        std::cout << "found";
    }
    return 0;
}

Expected output

found

Practice Compute the total sales

Take the previous step's std::sort and std::find calls. Add #include <numeric> and compute the total of the sales vector with std::accumulate, then print it. Print the total on a new line after the "found" message.

Starter code

main.cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

int main() {
    std::vector<int> sales = {150, 80, 200, 90};
    std::sort(sales.begin(), sales.end());
    auto it = std::find(sales.begin(), sales.end(), 150);
    if (it != sales.end()) {
        std::cout << "found";
    }
    // Print the total with std::accumulate
    return 0;
}

Sample solution

main.cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

int main() {
    std::vector<int> sales = {150, 80, 200, 90};
    std::sort(sales.begin(), sales.end());
    auto it = std::find(sales.begin(), sales.end(), 150);
    if (it != sales.end()) {
        std::cout << "found";
    }
    int total = std::accumulate(sales.begin(), sales.end(), 0);
    std::cout << total;
    return 0;
}

Expected output

found520

STL algorithms recap

STL algorithms are generic functions that work on containers. std::sort sorts, std::find searches, std::count counts, std::transform maps, std::accumulate sums. All work with iterator ranges.

Their power is that they are independent of the container type. The same std::sort works on both std::vector and std::array. In the next group you will learn lambda expressions, which make passing conditions and operations to STL algorithms much more powerful.

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which algorithm finds the total of a vector?

main.cpp
std::vector<int> sales = {150, 80, 200};
  • std::accumulate Correct answer
  • std::sort
  • std::find
  • std::count

Explanation: std::accumulate sums the elements.

2 Complete the sort call

Question
std::____(sales.begin(), sales.end());
Answer
std::sort(sales.begin(), sales.end());

Explanation: std::sort sorts the sales.

All lessons in Intermediate