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

How to insert 3 elements or another odd number of elements in an unordered_map?

本文关键字:元素 其他 插入 unordered map      更新时间:2023-10-16

我正在开发某种交通应用程序,我有车站,我把它们之间的连接放在一个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} 对,但我没有工作。

您应该将连接定义为结构,然后将其插入到某个有意义的 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"});

这将允许您稍后通过连接 ID 检索结构。

相关文章: