Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate through unordered map C++

I have wrote program which reads input until you hit ',' - COMA at the input. Then it counts the number of letters you put in,

I want to iterate through this map but it says that it cannot be defined with no type:

#include <iostream>
#include <conio.h>
#include <ctype.h>

#include <iostream>
#include <string>
#include <tr1/unordered_map>
using namespace std;

int main(){
    cout << "Type '.' when finished typing keys: " << endl;
    char ch;
    int n = 128;
    std::tr1::unordered_map <char, int> map;

    do{
      ch = _getch();
      cout << ch;
      if(ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z'){
        map[ch] = map[ch] + 1;
      }
    } while( ch != '.' );

    cout << endl;

    for ( auto it = map.begin(); it != map.end(); ++it ) //ERROR HERE
      cout << " " << it->first << ":" << it->second;


    return 0;
}
like image 208
Yoda Avatar asked Apr 05 '14 11:04

Yoda


People also ask

How do I iterate through an unordered map?

Iterating over a map by using STL Iterator: By creating an iterator of std::map and initializing it to the starting of map and visiting upto the end of map we can successfully iterate over all the elements of map.

How do I compare unordered maps?

The comparison between unordered_map objects is not affected by the arbitrary order in which they store their elements. Two unordered_maps are equal if they have the same number of elements and the elements in one container are a permutation of the elements in the other container. Otherwise, they are unequal.

Does unordered_map maintain insertion order?

No. It's called "unordered" for a reason. If you need to maintain an order of insertion, you've chosen an unsuitable data structure.

Can we sort unordered_map in C++?

Can we sort the unordered_map on the basis of VALUE in c++ using inbuilt sort function ? This is impossible from both a compilation and logical standpoint.


3 Answers

With C++17 you can use a shorter, smarter version, like in the code below:

unordered_map<string, string> map;
map["hello"] = "world";
map["black"] = "mesa";
map["umbrella"] = "corporation";
for (const auto & [ key, value ] : map) {
    cout << key << ": " << value << endl;
}
like image 62
Dorin Lazăr Avatar answered Oct 11 '22 21:10

Dorin Lazăr


You are using auto so you have C++11 code. You need a C++11 compliant compiler (e.g. GCC 4.8.2 or newer). As Peter G. commented, don't name your variable map (which is std::map) but e.g. mymap So please

#include <unordered_map>

(no need for tr1!)

Then compile with g++ -std=c++11 -Wall -g yoursource.cc -o yourprog and code a range based for loop

for (auto it : mymap) 
    std::cout << " " << it.first << ":" << it.second << std::endl;
like image 40
Basile Starynkevitch Avatar answered Oct 11 '22 19:10

Basile Starynkevitch


Add -std=c++11 to your compiler flags (with gcc/icc/clang) if you want to use auto (and other C++11 features). Btw, unordered_map is in std in C++11 ... Also there is std::isalpha ...

like image 29
Walter Avatar answered Oct 11 '22 19:10

Walter