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 <set>
#include <string>

int main() {
    std::multiset<int> numbers = {5, 2, 8, 2, 1, 9, 3, 7, 3};

    // Inserting elements
    numbers.insert(4);
    numbers.insert(2);  // Duplicate allowed

    // Printing the multiset
    std::cout << "Numbers in the multiset:" << std::endl;
    for (const auto& num : numbers) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    // Counting occurrences
    std::cout << "Count of 2: " << numbers.count(2) << std::endl;

    // Finding elements
    auto range = numbers.equal_range(3);
    std::cout << "Elements equal to 3: ";
    for (auto it = range.first; it != range.second; ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    // Removing an element
    numbers.erase(numbers.find(3));  // Removes only one instance of 3

    // Size of the multiset
    std::cout << "Number of elements: " << numbers.size() << std::endl;

    return 0;
}
Back to std_multiset