example1_basic_usage.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <map>
#include <string>

int main() {
    std::multimap<std::string, int> studentScores;

    // Inserting elements
    studentScores.insert({"Alice", 85});
    studentScores.insert({"Bob", 90});
    studentScores.insert({"Alice", 92});  // Another score for Alice
    studentScores.insert({"Charlie", 88});
    studentScores.insert({"Bob", 95});    // Another score for Bob

    // Iterating through the multimap
    for (const auto& pair : studentScores) {
        std::cout << pair.first << ": " << pair.second << std::endl;
    }

    // Counting entries for a specific key
    std::string name = "Alice";
    std::cout << "Number of scores for " << name << ": " 
              << studentScores.count(name) << std::endl;

    // Finding all scores for a specific student
    auto range = studentScores.equal_range(name);
    std::cout << name << "'s scores: ";
    for (auto it = range.first; it != range.second; ++it) {
        std::cout << it->second << " ";
    }
    std::cout << std::endl;

    // Size of the multimap
    std::cout << "Total number of scores: " << studentScores.size() << std::endl;

    return 0;
}
Back to std_multimap