c++multisetequal-range

C++ operations on values given by multiset::equal_range


I'm trying to write a program that is taking 1000 random numbers from 0 to 9 and then counting how many times each number appeared:

    srand(time(NULL));
    multiset<int> M;//multiset that contains 1000 random numbers from 0 to 9
    for (int i = 0; i < 1000; i++){
        r.insert(rand() % 10);
        s.insert(rand() % 10);
    }

    vector <int> R(10);//vector that stores how many times each number appeared 
    //(so R[0] equals how many times 0 appeared and so on)

    pair<multiset<int>::iterator, multiset<int>::iterator> zero = M.equal_range(0);
    R[0] = zero.second - zero.first;

The problem is in the last line (in which I'm trying to count the number of times 0 appeared), it underlines minus and says that no operator matches these operands. But why? Aren't zero.first and zero.second ends of compartments? And how to fix it?

Edit: I have to use a multiset, vector and equal_range, these are the things my teacher picked, not me.


Solution

  • Use distance(zero.first, zero.second). The - operator cannot be applied to multiset iterators.