Main idea
STL gives you tested containers and algorithms. Your job is to choose the right one and keep the code readable.
In a hackathon project, STL saves time and reduces bugs. You should not write your own dynamic array, sorting function, or map unless the problem asks for it.
What STL means
STL is the C++ standard library part you use for common data work. It includes containers like vector, map, set, queue, and algorithms like sort, find, count, and binary_search.
Think of it as your project toolbox. If your app stores expenses, votes, tasks, marks, or UPI-like transactions, STL probably has the data structure you need.
Choose containers
Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.
0 of 5 done
Expense summary example
Here is a small pattern you can use in a project. It stores spends by category, sorts them, and prints the biggest categories first.
This is useful for a hostel expense tracker, event budget tool, or any project with grouped totals.
#include <algorithm>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
int main() {
vector<pair<string, int>> expenses = {
{"food", 120}, {"travel", 40}, {"food", 80},
{"printing", 30}, {"travel", 60}
};
unordered_map<string, int> total;
for (const auto& item : expenses) {
const string& category = item.first;
int amount = item.second;
total[category] += amount;
}
vector<pair<string, int>> report(total.begin(), total.end());
sort(report.begin(), report.end(), [](const auto& a, const auto& b) {
return a.second > b.second;
});
for (const auto& row : report) {
cout << row.first << ": Rs " << row.second << '\n';
}
}Use algorithms first
Before writing a loop, ask if an STL algorithm already does it. This makes your intent clear to a reviewer reading your GitHub repo.
Common picks: sort for ordering, find for search, count_if for filtering count, accumulate for totals, and lower_bound for binary search on sorted data.
#include <algorithm>
#include <numeric>
#include <vector>
using namespace std;
vector<int> marks = {55, 82, 41, 90, 76};
sort(marks.begin(), marks.end());
int passed = count_if(marks.begin(), marks.end(), [](int x) {
return x >= 50;
});
int total = accumulate(marks.begin(), marks.end(), 0);Common mistakes
Do not use lower_bound on an unsorted vector. It only works correctly when the range is sorted in the same order you search.
Do not keep iterators or references after changing a vector size with push_back if the vector may reallocate. Use indexes, or finish adding data before storing references.
Do this now
Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.
0 of 5 done