| | |
hashing size problem
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Mar 2005
Posts: 3
Reputation:
Solved Threads: 0
My code is below. I'm pretty sure my issues are spanning my my .resize(). It's possible I'm doing my hashing completely wrong too, but it was right at one point. This code wont run at all right now, but it compiles. Is there a better way to go about sizing my container?
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <fstream> #include <vector> #include <list> using namespace std; typedef vector<list<string> > hash_set; void hashInsert(hash_set &vtr, string word); int getHash(string word); int main() { int size = 0; string word; hash_set vtr; ifstream firstfile ("doc1.txt"); if (firstfile) { while ( firstfile >> word ) { size++; vtr.resize(2*size); // hashInsert(vtr, word); } firstfile.close(); } else cout << "Unable to open file"; ifstream samefile ("doc1.txt"); if (samefile) { while ( samefile >> word ) { hashInsert(vtr, word); } samefile.close(); } else cout << "Unable to open file"; for (size_t vtrIndex=0; vtrIndex < vtr.size(); ++vtrIndex) { for (list<string>::iterator it = vtr[vtrIndex].begin(); it != vtr[vtrIndex].end(); ++it) { cout << *it << " " << vtrIndex << '\n'; } } return 0; } void hashInsert(hash_set &vtr, string word) { vtr[getHash(word)].push_back(word); } int getHash(string word) { int output; int i; output = word[word.size()-1]; for (i = (word.size()-2); i >= 0; i--) { output = (128*output + word[i]) % 127; } return output; }
Something more than "This code wont run at all right now" would be helpful. 
I don't see why you would want to keep resizeing your vector. It's simply not needed to start with. I would suggest just comment out that code, let vector resize by itself, later when things work you can optimize.
Anyway I don't think there is any issue with resize(). Also given the way you're storing teh data (vector<list<..>> ) I don't see any problems that your hash function can cause.
-------------
O now I saw your hashing code more closely. Well, given that you have done this:
output = (128*output + word[i]) % 127;
It's always ensured that getHash() returns something between 0-127. So line 20 to 31 in your original code can be replaced by vtr.resize(128) ;

I don't see why you would want to keep resizeing your vector. It's simply not needed to start with. I would suggest just comment out that code, let vector resize by itself, later when things work you can optimize.
Anyway I don't think there is any issue with resize(). Also given the way you're storing teh data (vector<list<..>> ) I don't see any problems that your hash function can cause.
-------------
O now I saw your hashing code more closely. Well, given that you have done this:
output = (128*output + word[i]) % 127;
It's always ensured that getHash() returns something between 0-127. So line 20 to 31 in your original code can be replaced by vtr.resize(128) ;
Last edited by thekashyap; Mar 22nd, 2007 at 1:10 pm.
![]() |
Similar Threads
- Font Size Problem (PHP)
- friends problem, HJT log (Viruses, Spyware and other Nasties)
- string size problem (C)
- string size problem (C)
Other Threads in the C++ Forum
- Previous Thread: population program
- Next Thread: error C2375: 'my_strdup' : redefinition; different linkage
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





