在c++中使用一个map中的值作为另一个map中的键
Using value from one map as key in another map in C++
我有一个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;
}
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 运行同一解决方案的另一个项目的项目
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- C++从另一个类访问公共静态向量的正确方法是什么
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 使用std::transform将一个范围的元素添加到另一个范围中
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 我可以将一个用clang c++11编译的对象与另一个用c++17编译的对象链接起来吗
- 修改函数中的指针(将另一个指针作为参数传递)
- 为什么在 std::map 上移动无法将元素从一个映射移动到另一个映射
- 如何有效地将(一些)项目从一个std::map移动到另一个std::map
- 我可以将 std::map 的内容移动分配到另一个 std::map 中吗?
- 检查map<string,string>是否包含另一个map<string,string>
- std::map插入另一个map
- 将类成员 (Map) 的指针作为默认参数传递给另一个类方法
- 如果键已经存在,为什么 stl map 会插入另一个值,而不仅仅是更改它
- 如何将所有对从一个std::map移动到另一个std::map
- 在c++中使用一个map中的值作为另一个map中的键
- 如何将 std::map 元素从一个减去到另一个并在 C++ 中更新它