STL layer
- C++ course
- Intermediate
- Lesson 114/116
- 6 min
Write data filtering with STL containers and algorithms.
STL layer: vector
In this lesson we collect Task objects in a std::vector. A std::vector is a dynamic array; you add elements with push_back.
Example: std::vector<Task> tasks; tasks.push_back(Task("Homework"));. This keeps tasks in a list. You access the first element with tasks[0]. STL algorithms work on this vector: std::count_if counts elements that satisfy a condition. We take the Task class and printItem template from previous lessons and add the vector layer on top.
count_if and lambda
std::count_if counts elements in a vector that satisfy a condition. The condition is given as a lambda.
Example: std::count_if(tasks.begin(), tasks.end(), [](const Task& t) { return t.done; });. Here the lambda checks t.done for each task. If true the task counts as done. A lambda is a short way to write small anonymous functions. [](const Task& t) { return t.done; } takes a Task and returns its done flag. This pattern is very common with STL algorithms.
Practice Add tasks to a vector and count done
After the Task class and printItem template in the starting code, create a std::vector<Task> in main. Add three tasks (Homework, Sport, Book). Mark the first done with tasks[0].done = true;. Count completed tasks with std::count_if and print the count with std::cout.
Starter code
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
class Task {
public:
std::string title;
bool done;
Task(std::string t) : title(t), done(false) {}
};
template <typename T>
void printItem(const T& item) {
std::cout << item;
}
int main() {
// Add three tasks to a vector, mark one done, count and print
return 0;
}Sample solution
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
class Task {
public:
std::string title;
bool done;
Task(std::string t) : title(t), done(false) {}
};
template <typename T>
void printItem(const T& item) {
std::cout << item;
}
int main() {
std::vector<Task> tasks;
tasks.push_back(Task("Homework"));
tasks.push_back(Task("Sport"));
tasks.push_back(Task("Book"));
tasks[0].done = true;
int n = std::count_if(tasks.begin(), tasks.end(),
[](const Task& t) { return t.done; });
std::cout << n;
return 0;
}Expected output
1
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Match each piece to its role
| Term | Meaning |
|---|---|
std::vector<Task> |
Task list |
push_back |
Adding an element |
tasks[0] |
First element |
std::count_if |
Counting by condition |
2 Complete the vector definition
std::____<Task> tasks;
std::vector<Task> tasks;
Explanation: std::vector is a dynamic array.
3 What does std::count_if do?
std::count_if(tasks.begin(), tasks.end(),
[](const Task& t) { return t.done; });- Counts those satisfying the condition Correct answer
- Deletes the vector
- Returns the first element
- Sorts the vector
Explanation: count_if counts elements satisfying the condition.