Tsallaka zuwa abun ciki
Justice Tech School

Maps and Sets

map stores sorted key and value pairs. unordered_map is usually faster but unsorted. set keeps unique, sorted values. Use count or find to test for a key.

Example
map<string, int> stock;
stock["apple"] = 5;
stock["mango"] = 12;
stock["apple"] = 7;
for (auto& [name, qty] : stock) {
  cout << name << "=" << qty << endl;
}
cout << stock.count("kiwi") << endl;
set<int> s = {3, 1, 3, 2, 1};
cout << s.size() << endl;

Result

apple=7
mango=12
0
3

Exercise

Test whether a key exists.

stock.("apple")

Quiz Test

How does a set treat duplicates?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.