jeudi 31 octobre 2019

Print duplicate values once in a multimap

I have a multimap that contains using myMap= std::multimap <std::string, std::string>;

here is a sample data that's in it.

West;Tuulensuu
West;Keskustori
South;Sammonaukio
West;Rautatieasema
West;Tulli
West;Sammonaukio
East;Sammonaukio

I'd like to print just the values, but all the values should only be printed once and should be sorted.

I've tried the following.

void print_lines(myMap& tramlines)
{
    for (auto it = tramlines.begin(); it != tramlines.end(); ) {
        std::string key = it->second;

        std::cout << it->second << std::endl;

        // Advance to next non-duplicate entry.
        do {
            ++it;
        } while (it != tramlines.end() && key == it->second);
    }
}

It still printed the values more than once. So I tried the following.

auto uniq = std::unique(tramlines.begin(), tramlines.end());
tramlines.erase(uniq, tramlines.end());

for(auto item : tramlines)
{
    std::cout << item.second << std::endl;
}

But that's giving me the following error

Severity    Code    Description Project File    Line    Suppression State
Error   C2679   binary '=': no operator found which takes a right-hand operand of type 'std::pair<const _Kty,_Ty>' (or there is no acceptable conversion)   Prog2Test

What are my options here? I can't find anything online that points to printing values, I've found plenty for Keys.

Would iterating through the values and adding them to set and printing them out a good idea?

Aucun commentaire:

Enregistrer un commentaire