简体   繁体   中英

Display Hash-Map Value C++

Simple program to create a hash-map and output its key values.

std::hash_map<int, std::vector<char>> numPad;

for (auto const key_1: numPad) 
    {
      for (auto const key_2: key_1.second) 
      {        
        std::cout << key_1; // Does NOT work! (*error*)
        std::cout << key_2; // Works!
      }
    }

I am getting the following errors:

( error ) Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const std::pair<const _Kty,_Ty>' (or there is no acceptable conversion)

How do I display the key_2 value without getting this error?

When used in a ranged-based for loop an std::map will iterate using std::pair . And because there is no match for std::cout for an std::pair you get an error, so instead of using key_1 directly, try key_1.first to get key value of the map or key_1.second to get the mapped value of the map.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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