简体   繁体   English

如何在哈希图中访问键的值

[英]How to access value of a key in hash map

I am trying to access the value of particular hash key. 我正在尝试访问特定哈希键的值。 The example code is below. 示例代码如下。 Test Here 在这里测试

// unordered_map::at
#include <iostream>
#include <string>
#include <unordered_map>

int main ()
{
  std::unordered_map<std::string,int> hashmap = {
                { "Apple", 300},
                { "Banana", 60},
                { "Orange", 70 } };

  std::cout << "Value :" << hashmap[300]<<std::endl;

  return 0;
}

However when I try to access key of a particular value, it works fine, like hashmap["Apple"] , it give 300 i,e the key of Apple . 但是,当我尝试访问特定值的键时,它可以正常工作,就像hashmap["Apple"] ,它给出300 i,e是Apple的键。 How to make it work otherwise like hashmap[300] to give "Apple" . 如何使它工作,像hashmap[300]给“ Apple”。

How to make it work otherwise like hashmap[300] to give "Apple" . 如何使它工作,像hashmap[300]"Apple"

Hash maps are unidirectional: key --> value . 哈希映射是单向的: key --> value If you need both directions to be fast, you'll need a different data structure (like Boost.Bimap ). 如果您需要两个方向都很快,则需要一个不同的数据结构(例如Boost.Bimap )。

If you just want that lookup to work period and are okay with linear performance, than you can just use std::find_if : 如果您只希望该查找工作期并且线性性能还可以,则可以使用std::find_if

auto it = std::find_if(hashmap.begin(), hashmap.end(),
    [](auto const& pr){ return pr.second == 300; });
if (it != hashmap.end()) {
    std::cout << "Key for 300: " << it->first;
}

There's nothing in std::unordered_map that based on looking up values. std::unordered_map中没有任何基于查找值的内容。

There is no direct access as you expected. 没有您期望的直接访问。

One of the ways is to use std::find_if . 一种方法是使用std::find_if

//DATA
std::unordered_map<std::string,int> hashmap = {

            {"Apple", 300},
            {"Banana",60},
            {"Orange",70}
};


//FIND BASED ON GIVEN INPUT
const auto& foundItem = std::find_if(hashmap.begin(),hashmap.end(),
                [](const std::pair<std::string,int>& item)
                {
                    return item.second == 300;
                });

std::cout<<foundItem->first<<std::endl;

And moreover, when you are looking for value in a unordered map, there is a possibility of another key having similar value. 而且,当您在无序映射中寻找价值时,可能会有另一个价值相似的钥匙。

for example another element {"grape",300} . 例如另一个元素{"grape",300} If you want all the keys with value 300.... 如果您希望所有键的值为300 ....

//DATA
std::unordered_map<std::string,int> hashmap = {

              {"Apple", 300},
              {"Banana",60},
              {"Orange",70},
              {"Grape",300}
};

//RESULT
std::vector<std::string> foundValues;

//FIND BASED ON GIVEN INPUT
std::for_each(hashmap.begin(),hashmap.end(),
                 [&foundValues](const std::pair<std::string,int>& item)
                 {
                       if(item.second == 300)
                           foundValues.push_back(item.first);

                 });

 //JUST TO TEST
 std::cout<<foundValues[0]<<"   "<<foundValues[1]<<std::endl;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM