c++c++11multiset

Iterating over unique elements of `std::multiset`


All I need is to know if something exists and how many times it exist. I will iterate over the existent things and query how much of that exists.

My implementation so far uses multiset, I do as follow:

std::multiset<thing> a;
auto previous = a.end();
for( auto each = a.begin(); each != a.end(); ++each ) {
    if( previous == a.end() || *previous != *each ) {
        a.count(*each);
    }
    previous = each;
}

Clarifications

I have a vector of things. But they repeat the value sometimes, I want to iterate over unique things and for each unique do something. This "something" needs to know the amount of time this thing appears on the vector.

The code I posted above is how I am resolving my problem right now, it does not seems to be the most elegant way to do what I want.

I am just following the Stackoverflow guidelines: I tell what is my problem, and I tell my (tried) solution.

If a sentence with a question mark is really needed, there you go: Is there a way to iterate over unique elements over a multiset?


Solution

  • Three possible approaches: