从包含unordered_maps的unordered_map中提取信息

问题描述 投票:0回答:2

我的问题解释起来有点复杂。所以我会尽量让自己变得清晰。我正在开发类似Citymapper的C ++应用程序实现。在当前级别,我使用两个unordered_maps,其中一个嵌套在另一个中,最后是一对矢量。我还有两个.csv文件,其中一个是不同的地铁站,一个是unorder_map的键和另外的其他信息,另一个是包含不同站点之间的连接(出发的关键,到达的关键,travel_time)。我指定compute_travel计算直接连接的两个站。我试图从_start和_end拉出两个站点之间的travel_time(from,to)或(_start,_end in uint64)。我实现了两个函数:compute_travel和compute_and_display_travel。第一个提取travel_time,第二个显示站之间的移动。

这是毫不拖延的(借口我的法语):

vector<pair<uint64_t,uint64_t> > Station_parser:: compute_travel(uint64_t _start, uint64_t _end){
    vector<pair<uint64_t, uint64_t> > vect; //RA1I ?

    int travel_time=0; //RA1I 
    for(auto& j:connections_hashmap){
        for(auto&i:(j.second)){//pour chaque noeud de l'unordered_map connections de connections_hashmap
            if ((i.first==_start)&&(i.second==_end)){ //on recherche le couple départ-destination 
                 travel_time=j.first; //on récupère le travel_time de la connection répond au critère
            }


            else
                cout<<"Erreur"<<endl;
        }
    }

    vect.push_back(make_pair(_start,travel_time));
    return vect;  
}


vector<std::pair<uint64_t,uint64_t> > Station_parser::compute_and_display_travel(uint64_t _start, uint64_t _end){
    vector<pair<uint64_t, uint64_t> > vect=compute_travel(_start,_end);
    for(auto &i:vect){

         cout << i.first << "," << i.second << endl;
    }
    return vect;
}

我的代码编译但我的travel_time设置为0,好像程序没有进入最后一个循环(这是不正常的)。我应该得到我的.csv文件中的travel_time。谢谢你的帮助。

c++ vector unordered-map
2个回答
0
投票

我想我已经明白你要做什么了。我不明白你的意思是“不经历最后的循环”。这样的代码看起来还不错。但是,在我看来,不应该在这个例子中使用地图。此外,似乎有不需要的副本,矢量分配,......但让我们考虑所有这些超出范围。

最有可能的是,您的数据被错误地加载到地图中(您没有显示如何从输入文件加载数据)。这也可以解释为什么你的旅行时间设置为0:你初始化int travel_time=0;,如果没有在connections_hashmapj.second中找到元素,或者如果((i.first==_start)&&(i.second==_end))返回false,那么travel_time将保持等于0并被推入vector你回来了。

我尝试了这个小例子,表明你的代码应该正常工作,并且你用正确的参数调用你的函数startend(你应该添加一些例外或默认行为 - 当前代码中的默认行为是设置你的时间到0)。

// time, start, end
std::unordered_map<int,std::unordered_map<int,int>> connections;

std::vector<std::pair<int,int>> compute_travel(int start, int end)
{
    std::vector<std::pair<int,int>> vect;

    int travel_time=0;

    // what if connections is empty?
    for(const auto& [time,connectedStations] : connections)
    {
        // what if connectedStations is empty?
        for(const auto& [s,e] : connectedStations)
        {
            if ( s == start && e == end)
                 travel_time = time;
            else // and what happens here?
                std::cout<< "Erreur" << std::endl;
        }
    }

    vect.push_back(std::make_pair(start,travel_time));
    return vect;  
}


std::vector<std::pair<int,int> > compute_and_display_travel(int start, int end)
{
    std::vector<std::pair<int,int> > vect = compute_travel(start,end);
    for(const auto& i : vect)
        std::cout << "depart station: " << i.first << ", time: " << i.second << std::endl;
    return vect;
}

int main()
{
    // 4 mins from station 1 to station 2
    connections[4][1] = 2;
    compute_and_display_travel(1,2);

    return 0;
}

// output: depart station: 1, time: 4

-1
投票

谢谢,我实际上不明白现在发生了什么。我使用两个功能:读取站和读取连接。读取工作正常(重载<<运算符验证),但它就像read_connections“甚至不工作”。我在函数的第一行插入了一个打印但不打印。这是代码:

        void Station_parser::read_connections(const std::string& _filename){
    cout<<"hi"<<endl; //?????????
    ifstream entree(_filename.c_str()); 
    if (entree.fail()){
        cerr<<"Error"<<endl;
    }              
    string from=" ";
    string to=" ";
    string tfr=" ";//travel_time
    while(entree.good()){
        getline(entree,from,',');
        getline(entree,to,',');
        getline(entree,tfr,'\n'); //go to next line
        uint64_t fr=strtoul(from.c_str(),NULL,10);//base 10
        uint64_t t=strtoul(to.c_str(),NULL,10);
        uint64_t tf_time=strtoul(tfr.c_str(),NULL,10);
        connections.insert({fr,t});
        connections_hashmap.insert({tf_time,connections});
    }
    entree.close();
}

不幸的是我不能使用调试器,因为我正在使用notepad ++并在linux bash上编译。我试图使用code :: blocks但它没有构建,这很难过。

© www.soinside.com 2019 - 2024. All rights reserved.