0Pricing
C++ Academy · Lesson

multimap and multiset

Allow duplicate keys.

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;
}

All lessons in this course

  1. std::map
  2. std::set
  3. multimap and multiset
  4. Custom Comparators
← Back to C++ Academy