darthshak
November 4th, 2008, 02:26 PM
Hi.
I was using a hash table of characters for this codec that I was programming. However, this is my first brush with the hash_map structure defined in the STL.
Here's the code I'm using :
#include <iostream>
#include <fstream>
#include <string>
#include <ctype.h>
#include <utility>
#include <ext/hash_map>
using namespace std;
using namespace __gnu_cxx;
struct eqchar {
bool operator()(char c1, char c2) {
return c1 == c2;
}
};
typedef hash_map<char,int,hash<char>,eqchar> table;
typedef pair<char,int> entry;
void findFrequency() {
table T; entry E;
string str; table::iterator itr;
ifstream file("sample.txt");
for (int i = 0; i < 255; i++) {
E.first = char(i);
T.insert(E);
}
while (!file.eof()) {
getline(file,str);
for (int i = 0; i < str.size(); i++) {
itr = T.find(str[i]);
(*itr).second += 1;
}
}
file.close();
}
int main() {
findFrequency();
return 0;
}
Well, the deal is in the line which says E.first = char(i). Now, the hash_map is a pair associative container of the form pair<key,data>. The problem is, by default, E.second assumes the value of 0.
Now, if all the keys are associated with the value 0, the worst-case complexity of searching becomes O(n), which defeats the purpose of hashing. Or is there some internal mechanism of hash_map which prevents this from happening?
Would adding the line E.second = i make a difference?
I was using a hash table of characters for this codec that I was programming. However, this is my first brush with the hash_map structure defined in the STL.
Here's the code I'm using :
#include <iostream>
#include <fstream>
#include <string>
#include <ctype.h>
#include <utility>
#include <ext/hash_map>
using namespace std;
using namespace __gnu_cxx;
struct eqchar {
bool operator()(char c1, char c2) {
return c1 == c2;
}
};
typedef hash_map<char,int,hash<char>,eqchar> table;
typedef pair<char,int> entry;
void findFrequency() {
table T; entry E;
string str; table::iterator itr;
ifstream file("sample.txt");
for (int i = 0; i < 255; i++) {
E.first = char(i);
T.insert(E);
}
while (!file.eof()) {
getline(file,str);
for (int i = 0; i < str.size(); i++) {
itr = T.find(str[i]);
(*itr).second += 1;
}
}
file.close();
}
int main() {
findFrequency();
return 0;
}
Well, the deal is in the line which says E.first = char(i). Now, the hash_map is a pair associative container of the form pair<key,data>. The problem is, by default, E.second assumes the value of 0.
Now, if all the keys are associated with the value 0, the worst-case complexity of searching becomes O(n), which defeats the purpose of hashing. Or is there some internal mechanism of hash_map which prevents this from happening?
Would adding the line E.second = i make a difference?