在c++中使用一个map中的值作为另一个map中的键

Using value from one map as key in another map in C++

本文关键字:map 另一个 一个 c++      更新时间:2023-10-16

我有一个unordered_map AvailableRooms的酒店房间数。像

Sheridan -> 10
Marriot ->12

我有一个地图hotelOnDate存储,字符串日期转换为unix时间针对酒店。

142356789 -> Sheridan
142356749 -> Marriot

现在,当我尝试使用地图键访问酒店的unordered_map值时,我得到一个0。

for(auto it = hotelOnDate.begin(); it != hotelOnDate.end(); it++){
        std::cout<<AvailableRooms[it->second]<<std::endl;
    }

AvailableRooms["Sheridan"]给出了正确的输出10。我哪里做错了?

我在我的代码中尝试了同样的事情,似乎对我有效。

#include <iostream>
#include <string>
#include <unordered_map>
#include <map>
int main ()
{
  std::map<std::string,std::string> mymap = {
                { "Mars", "g"},
                { "Saturn", "h"},
                { "Jupiter", "i" } };
 std::unordered_map<std::string,std::string> imap = {{"g","1"},{"h","2"}}; 
 for (auto it = mymap.begin(); it != mymap.end(); it++) {
    std::cout << it->first << ": " <<imap[ it->second ] << std::endl;
  }
 }
  return 0;
}
相关文章: