Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ unordered_map compiling issue with g++

I am using g++ in Ubuntu

g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3

I have this code

#include<unordered_map>
using namespace std;

bool ifunique(char *s){
  unordered_map<char,bool> h;
  if(s== NULL){
    return true;
  }
  while(*s){
    if(h.find(*s) != h.end()){
      return false;
    }
    h.insert(*s,true);
    s++;
  }
  return false;
}

when I compile using

g++ mycode.cc

I got error

 error: 'unordered_map' was not declared in this scope

Am I missing something?

like image 629
icn Avatar asked Oct 19 '10 23:10

icn


People also ask

Is unordered_map faster than map C++?

Note: unordered_map container performs faster than map when they have to access an individual element by their key.

Is unordered_map initialized to 0?

The value object is value-initialized, not zero-initialized.

Are duplicates allowed in unordered_map?

We have discussed unordered_map in our previous post, but there is a limitation, we can not store duplicates in unordered_map, that is if we have a key-value pair already in our unordered_multimap and another pair is inserted, then both will be there whereas in case of unordered_map the previous value corresponding to ...

Which is more efficient map or unordered_map?

Insertion of spread keys in std::map tends to outperform std::unordered_map when map size is under 10000 elements. Insertion of dense keys in std::map doesn't present performance difference with std::unordered_map under 1000 elements. In all other situations std::unordered_map tends to perform faster.


2 Answers

If you don't want to to compile in C++0x mode, changing the include and using directive to

#include <tr1/unordered_map>
using namespace std::tr1;

should work

like image 199
Hugh Avatar answered Sep 20 '22 10:09

Hugh


In GCC 4.4.x, you should only have to #include <unordered_map>, and compile with this line:

g++ -std=c++0x source.cxx

More information about C++0x support in GCC.

edit regarding your problem

You have to do std::make_pair<char, bool>(*s, true) when inserting.

Also, your code would only insert a single character (the dereferencing via *s). Do you intend to use a single char for a key, or did you mean to store strings?

like image 41
逆さま Avatar answered Sep 22 '22 10:09

逆さま