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?
Note: unordered_map container performs faster than map when they have to access an individual element by their key.
The value object is value-initialized, not zero-initialized.
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 ...
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.
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
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?
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