multimap and multiset
Allow duplicate keys.
multimap and multiset is a free C++ Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Allowing Duplicates
std::multimap and std::multiset are like map and set but they allow duplicate keys. Everything stays sorted.
#include <iostream>
#include <set>
int main() {
std::multiset<int> ms{1, 2, 2, 3, 3, 3};
for (int x : ms) std::cout << x << ' ';
std::cout << '\n';
return 0;
}multiset Insertion
Every insert() into a multiset succeeds, even for repeated values, growing the container each time.
#include <iostream>
#include <set>
int main() {
std::multiset<std::string> ms;
ms.insert("a");
ms.insert("a");
ms.insert("a");
std::cout << ms.size() << " elements\n";
return 0;
}Counting Duplicates
count() now returns how many times a value appears, which can be more than 1.
#include <iostream>
#include <set>
int main() {
std::multiset<int> ms{5, 5, 5, 7, 9, 9};
std::cout << "5 appears " << ms.count(5) << " times\n";
std::cout << "9 appears " << ms.count(9) << " times\n";
return 0;
}Erasing All vs One
erase(key) removes every matching element. To remove just one, erase a single iterator from find().
#include <iostream>
#include <set>
int main() {
std::multiset<int> ms{1, 2, 2, 2, 3};
ms.erase(ms.find(2));
std::cout << "count of 2: " << ms.count(2) << '\n';
return 0;
}multimap Basics
std::multimap maps keys to values but lets one key map to many values. Note: it has no operator[].
#include <iostream>
#include <map>
int main() {
std::multimap<std::string, int> mm;
mm.insert({"fruit", 1});
mm.insert({"fruit", 2});
mm.insert({"veg", 3});
std::cout << mm.size() << " pairs\n";
return 0;
}Iterating a multimap
Iteration yields all pairs in sorted key order, including repeated keys.
#include <iostream>
#include <map>
int main() {
std::multimap<std::string, int> mm{{"a", 1}, {"a", 2}, {"b", 3}};
for (const auto& [k, v] : mm) {
std::cout << k << " => " << v << '\n';
}
return 0;
}equal_range
equal_range(key) returns a pair of iterators bounding all elements with that key, the standard way to read every value for a key.
#include <iostream>
#include <map>
int main() {
std::multimap<std::string, int> mm{{"x", 10}, {"x", 20}, {"y", 30}};
auto range = mm.equal_range("x");
for (auto it = range.first; it != range.second; ++it) {
std::cout << it->second << ' ';
}
std::cout << '\n';
return 0;
}Grouping Data
A multimap is ideal for grouping: many people can share the same city, for example.
#include <iostream>
#include <map>
int main() {
std::multimap<std::string, std::string> byCity{
{"NYC", "Alice"}, {"NYC", "Bob"}, {"LA", "Carol"}
};
auto r = byCity.equal_range("NYC");
for (auto it = r.first; it != r.second; ++it)
std::cout << it->second << '\n';
return 0;
}lower_bound on multiset
Ordered range queries still work. lower_bound finds the first element not less than the target.
#include <iostream>
#include <set>
int main() {
std::multiset<int> ms{1, 2, 2, 3, 4};
auto it = ms.lower_bound(2);
std::cout << "first >= 2 is " << *it << '\n';
return 0;
}Sorted Frequency List
Because elements stay sorted, a multiset naturally produces ordered output with repetition preserved.
#include <iostream>
#include <set>
int main() {
std::multiset<int> scores{90, 75, 90, 60, 75, 90};
for (int s : scores) std::cout << s << ' ';
std::cout << '\n';
return 0;
}Total vs Distinct
size() counts every element including duplicates, while iterating distinct keys requires skipping repeats yourself.
#include <iostream>
#include <set>
int main() {
std::multiset<int> ms{1, 1, 2, 3, 3};
std::cout << "total: " << ms.size() << '\n';
std::cout << "count of 1: " << ms.count(1) << '\n';
return 0;
}Quick Check
Test your understanding of erasing from a multiset.
Recap
You learned that multimap and multiset:
- allow duplicate keys while staying sorted
- use
equal_rangeto read all values sharing a key - have
erase(key)remove all matches, while erasing an iterator removes one
Next, you'll control ordering with custom comparators.
Frequently asked questions
Is the “multimap and multiset” lesson free?
Yes — the full text of “multimap and multiset” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
What will I learn in “multimap and multiset”?
Allow duplicate keys. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “multimap and multiset” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- std::map
- std::set
- multimap and multiset
- Custom Comparators