The Standard Library Lesson 15 of 17
C++ STL — map, set, sort and Algorithms
Use the Standard Template Library — map and unordered_map for lookups, set for unique items, pair, sort with custom rules, find, count, accumulate and other algorithms.
The Standard Template Library (STL) is a collection of ready-made containers and algorithms. Instead of writing your own sorting or searching code, you use tested, fast versions. It's a big reason C++ is loved in programming contests.
Containers at a glance
| Container | Use for |
|---|---|
vector | A list in order (most common) |
map | Key → value lookups, sorted by key |
unordered_map | Faster key → value lookups, no order |
set | Unique items, sorted |
queue, stack | First-in-first-out, last-in-first-out |
pair | Two values together |
map
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
map<string, double> prices;
prices["pandesal"] = 25;
prices["kape"] = 8;
prices["itlog"] = 9;
cout << "Kape: " << prices["kape"] << endl;
prices["kape"] = 10; // update
if (prices.count("asukal") == 0) cout << "No asukal" << endl;
for (const auto &[item, price] : prices) { // sorted by key
cout << item << " = " << price << endl;
}
return 0;
}
Output
Kape: 8 No asukal itlog = 9 kape = 10 pandesal = 25
for (const auto &[item, price] : prices) uses structured bindings (C++17) to unpack each key–value pair.
Counting with a map
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int main() {
vector<string> votes = {"Ana", "Ben", "Ana", "Carla", "Ana", "Ben"};
map<string, int> tally;
for (const string &v : votes) tally[v]++;
for (const auto &[name, count] : tally) {
cout << name << ": " << string(count, '*') << " " << count << endl;
}
return 0;
}
Output
Ana: *** 3 Ben: ** 2 Carla: * 1
tally[v]++ works even the first time, because a missing key starts at 0.
set
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main() {
set<string> barangays = {"Poblacion", "Rizal", "Mabini", "Rizal", "Poblacion"};
barangays.insert("San Isidro");
cout << "Unique: " << barangays.size() << endl;
for (const string &b : barangays) cout << b << " ";
cout << endl;
cout << boolalpha << (barangays.count("Mabini") > 0) << endl;
return 0;
}
Output
Unique: 4 Mabini Poblacion Rizal San Isidro true
sort and custom sorting
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
struct Student {
string name;
double average;
};
int main() {
vector<int> scores = {72, 95, 88, 60, 81};
sort(scores.begin(), scores.end());
for (int s : scores) cout << s << " ";
cout << endl;
sort(scores.begin(), scores.end(), greater<int>()); // descending
for (int s : scores) cout << s << " ";
cout << endl;
vector<Student> cls = {{"Ana", 91.5}, {"Ben", 84.0}, {"Carla", 95.2}};
sort(cls.begin(), cls.end(), [](const Student &a, const Student &b) {
return a.average > b.average; // highest first
});
for (size_t i = 0; i < cls.size(); i++) {
cout << i + 1 << ". " << cls[i].name << " " << cls[i].average << endl;
}
return 0;
}
Output
60 72 81 88 95 95 88 81 72 60 1. Carla 95.2 2. Ana 91.5 3. Ben 84
The third argument to sort is a comparison function, here a lambda, that returns true when a should come before b.
More algorithms
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
int main() {
vector<int> g = {88, 72, 95, 60, 81, 95};
cout << "Sum: " << accumulate(g.begin(), g.end(), 0) << endl;
cout << "Max: " << *max_element(g.begin(), g.end()) << endl;
cout << "Count of 95: " << count(g.begin(), g.end(), 95) << endl;
cout << "Failing: " << count_if(g.begin(), g.end(), [](int x) { return x < 75; }) << endl;
auto it = find(g.begin(), g.end(), 60);
if (it != g.end()) cout << "60 found at index " << (it - g.begin()) << endl;
reverse(g.begin(), g.end());
for (int x : g) cout << x << " ";
cout << endl;
return 0;
}
Output
Sum: 491 Max: 95 Count of 95: 2 Failing: 2 60 found at index 3 95 81 60 95 72 88
<algorithm> has dozens more: binary_search, unique, min_element, all_of, any_of, transform… Learning them saves a lot of code.
queue and stack
#include <iostream>
#include <queue>
#include <stack>
#include <string>
using namespace std;
int main() {
queue<string> line; // first in, first out
line.push("Ana"); line.push("Ben"); line.push("Carla");
cout << "Serving " << line.front() << endl;
line.pop();
cout << "Next: " << line.front() << endl;
stack<string> plates; // last in, first out
plates.push("Plate 1"); plates.push("Plate 2");
cout << "Top: " << plates.top() << endl;
return 0;
}
Output
Serving Ana Next: Ben Top: Plate 2
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04