8

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?

0

2 Answers 2

21

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

Sign up to request clarification or add additional context in comments.

Comments

15

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?

4 Comments

error: no matching function for call to 'std::unordered_map<char, bool, std::hash<char>, std::equal_to<char>, std::allocator<std::pair<const char, bool> > >::insert(char&, bool)'
@xlione: Can you show us the code? It seems like you're trying to insert a reference type into your map.
g++ -std=c++11 is the latest one now
i have check there is no need for pair while insertion, normally it will work.... like mp[i]=j; where mp is object of unordered_map.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.