多图未排序

发布于 2024-10-20 02:20:34 字数 544 浏览 4 评论 0 原文

我构建了这个多重映射来将字符串的汉明距离映射到其相应的字符串。

由于两个字符串的汉明距离可能相同,因此我希望它们按升序排序。但是当我打印出来时,它没有排序。 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;
            }

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;
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青巷忧颜 2024-10-27 02:20:34

多重映射中的元素按键(在本例中为无符号汉明距离)排序。具有相同键的元素不按值(在本例中为字符串)排序,它们通常按插入顺序保留。

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.

梨涡少年 2024-10-27 02:20:34

使用 std::multimap 是不可能的,因为当比较键时,不知道它们代表哪个值。

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

韬韬不绝 2024-10-27 02:20:34

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 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.

桃酥萝莉 2024-10-27 02:20:34

less 模板函数不是必需的,因为它是默认的。尝试将 Check 声明为不带:

typedef multimap<unsigned, string> Check;

已编辑:最好的方法是生成一个哈希键作为 *key_type* 并且 value-type 可以是 std::pair无符号,字符串>

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>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文