views:

26

answers:

2

i am using g++ in ubuntu

g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3 Copyright (C) 2009 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


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

something i am missing?

Thanks for any help!

+1  A: 

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?

birryree
xlione
@xlione: Can you show us the code? It seems like you're trying to insert a reference type into your map.
birryree
updated, thanks
xlione
issue is resolved,thanks!
xlione
+1  A: 

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

Huw Giddens
it works! thanks
xlione