Multimap not sorting

I have this multimap built to display the distance from line interference to the corresponding line.

Since the distance between the two lines may be the same, I want them to be sorted in ascending order. However, when I print it, it is not sorted. HamdistArray is declared as an unsigned type.

typedef multimap<unsigned, string, less<unsigned> > Check;
            Check pairs; 

            pairs.insert(Check::value_type(hamdistArray[j], d.sortedWordDatabase[j]));

            for(Check::const_iterator iter = pairs.begin(); iter != pairs.end(); ++iter)
            {
                cout << iter->first << '\t' << iter->second<< endl;
            }
+5
source share
4 answers

Elements in multimap are sorted by the key (in this case, the unsigned distance is hamming). Items with the same key are not sorted by value (in this case, a string), they are usually stored in the order in which they were inserted.

+4
source

, . Check as:

typedef multimap<unsigned, string> Check;

: - - * key_type *, std::pair<unsigned, string>

0

This is not possible with help std::multimap, because when the keys are compared, it is not known what value they represent.

0
source

multimapsorts only by its key (length), and not by value (string). In this case, I suspect your best approach is this std::map<unsigned, std::set<std::string> >. You can also use std::set<std::pair<unsigned, std::string> >, but for the search you will need to build a mannequin pairfor the search.

0
source

All Articles