多图未排序
我构建了这个多重映射来将字符串的汉明距离映射到其相应的字符串。
由于两个字符串的汉明距离可能相同,因此我希望它们按升序排序。但是当我打印出来时,它没有排序。 hamdistArray 被声明为无符号类型。
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
多重映射中的元素按键(在本例中为无符号汉明距离)排序。具有相同键的元素不按值(在本例中为字符串)排序,它们通常按插入顺序保留。
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.
使用 std::multimap 是不可能的,因为当比较键时,不知道它们代表哪个值。
This is not possible using
std::multimap
, because when keys are compared, it is not known which value they represent.multimap
仅按其键(长度)排序,而不按值(字符串)排序。在这种情况下,我怀疑你最好的方法是std::map; >
。您还可以使用 std::set对
来进行搜索。multimap
only sorts by its key (length) not also by value (string). In this case I suspect your best approach is astd::map<unsigned, std::set<std::string> >
. You could also use astd::set<std::pair<unsigned, std::string> >
but searching would require you to construct dummypair
s to search on.less 模板函数不是必需的,因为它是默认的。尝试将 Check 声明为不带:
已编辑:最好的方法是生成一个哈希键作为 *key_type* 并且 value-type 可以是
std::pair
无符号,字符串>
The less template function is not necessary since it's the default. Try declaring Check without as:
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>