0

I am writing a program that uses unordered map. after some research prior to this, I know that to use unordered map, first we need to put header. but It didn't worked. thanks in advance for any tips. and oh ya this is the error message

 #error This file requires compiler and library support for the \
  ^
umap.cpp: In function 'int main()':
umap.cpp:21:2: error: 'unordered_map' was not declared in this scope
  unordered_map<string, int> siswa;
  ^
umap.cpp:21:22: error: expected primary-expression before ',' token
  unordered_map<string, int> siswa;
                      ^
umap.cpp:21:24: error: expected primary-expression before 'int'
  unordered_map<string, int> siswa;
                        ^
umap.cpp:24:2: error: 'siswa' was not declared in this scope
  siswa["saleh"]=90;
  ^

AND here's the program

#include <iostream>
#include <unordered_map>
#include <bits/stdc++.h> 

using namespace std;

void cari(string key){

    if(siswa.find(key)==siswa.end())
        cout<<siswa[key]<<endl;
    else
        cout<<"n/a"<<endl;
}


int main(){


    unordered_map<string, int> siswa;


    siswa["saleh"]=90;
    siswa["mutiara"]=85;
    siswa["icam"]=70;

    int t;
    cin>>t;
    string key;
    for(int i=0;i<t;i++){

        getline(cin,key);
        cari(key);
    }
}

btw, sorry for my messy grammar :v

  • 2
    First, ```cout< – Eliott Robert Apr 06 '20 at 09:24
  • 3
    Don't `#include ` – NutCracker Apr 06 '20 at 09:25
  • 3
    What compiler do you use and what compiler flags do you pass? Also, [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Yksisarvinen Apr 06 '20 at 09:26
  • 2
    Your first error is incomplete and is a hint for the others. Have you enabled C++11? – molbdnilo Apr 06 '20 at 09:26
  • 2
    You need to compile with support for at least `c++11`. Also. please get rid of [`#include `](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [`using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – G.M. Apr 06 '20 at 09:26
  • 1
    you didnt `#include ` – 463035818_is_not_an_ai Apr 06 '20 at 09:28

1 Answers1

0

The scope error pertains to your unordered map not being accessible to the function cari within its scope, for which you can include an unordered map of corresponding data types as a parameter:

void cari(string key, unordered_map<string,int> x)
{
  if(x.find(key)==x.(end))
   cout<<x[key]<<"\n";
  else
   cout<<"n/a"<<"\n";
}

Other points to take note of:

  • As already addressed in the comments, avoid use of <bits/stdc++.h>. Its been mentioned thousand of times but its recurring usage is not a suprise since the newcomers in CP are misguided or have an inclination to use it with the most common reason being to save time writing the headers contained in the STL. But then you can always use a pre-written template when necessary. Additionally, most of the problems don't involve more than four headers so its not much of a hassle writing them within the contest duration either.
    The use of this header is discouraged primarily because it includes the whole STL family of headers, which makes the program redundant and in turn slow to compile as well. Moreover, although its supported in online-judges/compilers, its not supported in most IDEs (eg: Visual Studio) and compilers. Its usage is highly discouraged in production code as well, so take it as a point to avoid using it.

  • Use std::string or include <cstring>/<string.h> header. Note that if you don't declare namespace std (use of which again is considered as a bad practice), you'll have to use std:: as a prefix wherever required such as for cin, cout, string and unordered_map here. The parameters of the function would then look like:

void cari(std::string key, std::unordered_map<std::string,int> x)
  • Use \n instead of endl as the later invokes an unnecessary call to std::flush().

  • while(t--) is a better alternative than the for loop used to iterate over t (or the test cases I presume) as its shorter and discards use of an extra loop variable.

  • @MuhammadSaleh Consider accepting the answer (tapping the tick mark just below the votes) if it helps! –  Apr 16 '20 at 16:33