5

I have this multimap built to map the hamming distance of a string to its corresponding string.

Since the hamming distance of two strings could be the same, I want them to be sorted in ascending order. However when I print it out, it is not sorted. The 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;
            }
Xann
  • 51
  • 1
  • 2
  • 1
    `Check` is a silly name for this type. – Lightness Races in Orbit Mar 02 '11 at 13:40
  • I don't see how your posted code is a testcase, as you only insert one value. – Lightness Races in Orbit Mar 02 '11 at 13:40
  • @Tomalak Geret'kal The code is inside a for loop, hence the 'j'. Check was chosen because it corrects a string based on how close its hamming distance is from a database. – Xann Mar 02 '11 at 14:02
  • Then please include the loop in your testcase. `Check` is a silly name: the type does not correct anything; it's a type, not a procedure. It is the name of a type of data storage, not an algorithm that mutates data. – Lightness Races in Orbit Mar 02 '11 at 14:05
  • 1
    @Tomalak Geret'kal: I appreciate your insight. It might not make sense to you the name that I have chosen for this type. I hope it will not hinder your ability to help me answer my question though. :) – Xann Mar 02 '11 at 14:28

4 Answers4

6

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

kbjorklu
  • 1,338
  • 8
  • 10
  • 1
    Thank you for pointing that out. How would I sort the hamming distance together with its corresponding string then? Should I just do it manually? – Xann Mar 02 '11 at 14:07
  • Use set or multiset of pair of unsigned and string. – kbjorklu Mar 03 '11 at 14:48
  • The multimap docs seem to back up this answer: http://www.cplusplus.com/reference/stl/multimap/insert/ – Ogre Psalm33 Nov 20 '12 at 15:43
0

The less template function is not necessary since it's the default. Try declaring Check without as:

typedef multimap<unsigned, string> Check;

Edited: The best way to do this is to generate a hash-key as the *key_type* and than the value-type could be a std::pair<unsigned, string>

Felipe Tonello
  • 290
  • 3
  • 11
0

This is not possible using std::multimap, because when keys are compared, it is not known which value they represent.

Oswald
  • 31,254
  • 3
  • 43
  • 68
0

multimap only sorts by its key (length) not also by value (string). In this case I suspect your best approach is a std::map<unsigned, std::set<std::string> >. You could also use a std::set<std::pair<unsigned, std::string> > but searching would require you to construct dummy pairs to search on.

Mark B
  • 95,107
  • 10
  • 109
  • 188