C ++排序算法 [英] C++ sorting algorithm

查看:149
本文介绍了C ++排序算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您看这个问题提前。

Thanks for looking at this question in advance.

我想订购物品的清单如下:

I am trying to order the following list of items:

Bpgvjdfj,Bvfbyfzc
Zjmvxouu,Fsmotsaa
Xocbwmnd,Fcdlnmhb
Fsmotsaa,Zexyegma
Bvfbyfzc,Qkignteu
Uysmwjdb,Wzujllbk
Fwhbryyz,Byoifnrp
Klqljfrk,Bpgvjdfj
Qkignteu,Wgqtalnh
Wgqtalnh,Coyuhnbx
Sgtgyldw,Fwhbryyz
Coyuhnbx,Zjmvxouu
Zvjxfwkx,Sgtgyldw
Czeagvnj,Uysmwjdb
Oljgjisa,Dffkuztu
Zexyegma,Zvjxfwkx
Fcdlnmhb,Klqljfrk
Wzujllbk,Oljgjisa
Byoifnrp,Czeagvnj

为以下顺序:

Into the following order:

Bpgvjdfj
Bvfbyfzc
Qkignteu
Wgqtalnh
Coyuhnbx
Zjmvxouu
Fsmotsaa
Zexyegma
Zvjxfwkx
Sgtgyldw
Fwhbryyz
Byoifnrp
Czeagvnj
Uysmwjdb
Wzujllbk
Oljgjisa
Dffkuztu

这是通过:

  1. 以第一对​​,并把名字到一个列表
  2. 使用该对的第二名称,发现它被用作第一个名字的一对
  3. 加入了对第二个名字的名单
  4. 在重复2及3

我填充与对,然后排序并添加每个名称,以列表的unordered_map。这可以以下面的code可以看出:

I am populating an unordered_map with the pairs then sorting and adding each name to a list. This can be seen in the following code:

westIter = westMap.begin();
std::string westCurrent = westIter->second;
westList.push_front(westCurrent);

for(int i = 0; i < 30; i++)
{
    if(westMap.find(westCurrent) != westMap.end())
    {
        //find pair in map where first iterator is equal to "westCurrent"
        //append second iterator of pair to list
    }
    westIter++;
}

注:我不知道,如果push_front就是在这个时刻正确的,因为我只拿到了第一个值插入

Note: I'm not sure if "push_front" is correct at this moment in time as I have only got the first value inserted.

我的问题是可能有人给我一些见解,以我怎么能去呢?正如我不确定的最佳方式,以及是否我的想法是正确的。任何有识之士将AP preciated。

My question is could someone give me some insight as to how I could go about this? As I am unsure of the best way and whether my thinking is correct. Any insight would be appreciated.

推荐答案

有但在你的计划的一个弱点。你需要先找到链条中,先生纽约的第一人。

There is but one weakness in your plan. You need to first find the first person of the chain, the Mr New York.

您算法假定该行开始的第一个男人。对于工作,你应该先扫描整个地图,找到的一个不出现第二个元素的名称。这是先生纽约,你可以从那里继续。 的push_back 是什么你会需要在这里使用。

Your algorithm assumes the line starts with the first guy. For that to work, you should first scan the entire map to find the one name that does not appear as a second element. That is Mr New York and you can proceed from there. push_back is what you would need to use here.

这篇关于C ++排序算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆