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;
}
                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.
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.
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 the unordered_map on the basis of VALUE in c++ using inbuilt sort function ? This is impossible from both a compilation and logical standpoint.
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;
}
                        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;
                        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 ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With