如何在unordered_map中插入3个元素或其他奇数个元素?

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

我正在研究某种过境应用程序,我有站点,我把它们之间的连接放在unordered_map中。连接是:departure_station_id,arrival_station_id,travel_time。

如您所见,有三个要素。这是我已经尝试过的。

uint64_t fr=strtoul(from.c_str(),NULL,10);
uint64_t t=strtoul(to.c_str(),NULL,10);
uint64_t tf_time=strtoul(tfr.c_str(),NULL,10);
connections_hashmap.insert({{fr,t},tf_time});

我得到了这个:

 error: no matching function for call to ‘std::unordered_map<long unsigned int, std::unordered_map<long unsigned int, long unsigned int> >::insert(<brace-enclosed initializer list>)’                                                                     connections_hashmap.insert({{fr,t},tf_time});    

我也试过形成一个{tf_time,NULL}对,但我没有用。

c++ c++11 unordered-map
1个回答
0
投票

您应该将连接定义为结构,然后将其插入一些有意义的ID下:

struct connection {
  string departure_station_id;
  string arrival_station_id;
  string travel_time;
};

auto connections_hashmap = new unordered_map<string, connection>();
connections_hashmap.insert("connectionID", {"Powell", "Embarcadero", "3"});

这将允许您稍后通过connectionID检索结构。

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