multimap and multiset
Allow duplicate keys.
multimap and multiset is a free C++ Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.
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.
Learn C++ with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “multimap and multiset” lesson free?
Yes — any 3 lessons of the C++ Academy learning path, including “multimap and multiset”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
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