jeudi 31 octobre 2019

Sort unique values from a map

I have a custom map in my header file

class Tramway
{
private:
    using stations = std::vector <std::string>;
    using Tramlines = std::map <std::string, stations>;
...../

I'm trying to sort the unique values, but so far my approach is giving me compile errors. Here is my code.

void Tramway::print_stations(const Tramway::Tramlines &tramlines)
{

    for(auto map_iter = tramlines.cbegin(); map_iter != tramlines.cend(); ++map_iter)
    {
        std::unique(map_iter->second.cbegin(), map_iter->second.cend());

        std::sort(map_iter->second.begin(), map_iter->second.end());

       for( auto vec_iter = map_iter->second.cbegin() ; vec_iter != map_iter->second.cend() ; ++vec_iter )
           std::cout << *vec_iter << std::endl;
    }

}

Is there a way to get unique values and sort them at the same time? I tried ´std::sort(std::unique(..,..))´ but ´std::sort()´ needs two arguments to work.

Aucun commentaire:

Enregistrer un commentaire