Hi, I have a hash table consisting of char string. I do not know how to redo the numbers so I can upload to function Insert (9798), and compare them. Please help :)
I think you mean to process strings, but you are not. You are processing char pointers.
Conisder this code:
1 2
InsertHash("some test");
bool p = SearchHash("some test");
Functions InsertHash and SearchHash may or maynot be passed the same value. The compiler may chose to use String Pooling and use the same string in which case the two functions will receive the same value, or the compiler may not use that sort of optimisation and give you two different numbers.
The linked list class doesn't store strings either, it just stores char pointers.
Actually, it looks ok, because if you look at Lines 77-82 (implementation of Hash), the OP only calculates the hash on data[i], the individual characters, so that's fine. More troubling is the hardcoding of 10 on Line 79.
As for storing char pointers, that's ok, too, if the OP isn't trying to mimic the value-semantics of STL.
@derata
The only problem I had when compiling your program was:
warning: deprecated conversion from string constant to 'char*'
on two lines.
But if I convert all the char* to const char*, the warning goes away, and your code seems to run fine (as expected).
I don't quite understand your question, though.
Are you asking about how to update Insert() so it works even if you have a Hash-collision?
(Excuse my English, I'm from Czech Republic)
Thank you. I fixed it differently. The program now reads characters from the file. But knows his words, so that puts each character into its own list. But I do not want. I need to be one word in one list. And if the word is repeated by adding it to the list.
For example: "Mouse, Mouse: Car. Computer," contains a list of Mouse, List of two computer ...
Use it for calculating the entropy so I need to know the number of different words and the same number of words.
I thought that distinguish the following words:
But it does not work, because each letter written elsewhere. How to solve this problem?
1 2 3 4
character = file->get(); //read character
elsereturntrue; //other end of the word
if (((character>'a')&&(character<'z'))||((character>'A')&&(character<'Z')))
I do not know how this is done using maps :( as you have some sample code?
Using a simple input file. test.txt 'Mouse, car, mouse"
I have a problem with the fact that each character is stored separately. I need to save the whole word as an index of the table, another index of the next word. Same words, same index or the same word + +
example
mouse - index 1 , (twice)
car - index 2
na výstupu očekávám celkový počet slov, a počet stejných slov. To be able to perform calculations of entropy
ok - this should be enough for you to get what you need with a little modification
you do, however, need to read up and understand stl map, vector, and iterators
I am posting this code because with your initial code for Hash, I assume that you are either an advanced student or you have had other programming languages before - so I expect you to be able to understand this code with a little study. If you have questions, please post.
#include <iostream>
#include <string>
#include <fstream>
#include <map>
#include <vector>
usingnamespace std;
struct Position
{
vector< unsigned > m_positions;
};
int main()
{
map< string, Position > myMap; // myMap is like a hash whose key is string, value is Position
string word;
unsigned idx = 0; // the line-number of junk.txt starting from 0
ifstream ifs;
ifs.open( "junk.txt", ifstream::in );
if ( ifs.is_open() ) { // open file
while ( !ifs.eof() ) { // loop until eof
char character = static_cast<char>( ifs.get() ); // cast int back to char
if ( ifs.good() ) {
if ( isalpha( character )) { // still same word
word.append( 1, character ); // add the character to end of word
} else { // new word
if ( myMap.find( word )==myMap.end() ) { // not found
Position p; // instantiate a position record
p.m_positions.push_back( idx ); // add idx to the end of the vector
myMap[ word ] = p; // assign (copy) the value p to the key word in myMap
} else { // found
myMap[ word ].m_positions.push_back( idx ); // add idx to the end of the vector
}
word = ""; // reset because we're finished with word
idx++;
}
}
}
}
// output ---------------------------------------------------------------------
map< string, Position >::const_iterator it1; // declare an iterator for looping over map
vector< unsigned >::const_iterator it2; // declare an iterator for looping over vector
for ( it1=myMap.begin(); it1!=myMap.end(); ++it1 ) { // iterate over key, value in myMap
const string& key = it1->first; // it1->first points to the key
const Position& value = it1->second; // it2->second points to the value
for ( it2=value.m_positions.begin(); it2!=value.m_positions.end(); ++it2 ) { // iterate over m_positions
cout << key << " " << *it2 << endl; // it2 points to the value (item in the vector)
}
}
return 0;
}
vector is a contiguous chunk of memory, but it may grow - so it's a good replacement for array[]
map is a red-black tree, which technically is not a hashmap, but functions in a similar way
it allows you to map an object to another object ( string to Position ) in our case
I do not know how to translate into Czech "instantiate a position record"
1 2 3 4 5
if ( myMap.find( word )==myMap.end() ) { // not found
.........
} else { // found
myMap[ word ].m_positions.push_back( idx ); // add idx to the end of the vector
}
I do not know if I understand this: If I find the word. Give him the position. Assign the key value.
else:?
I have read something about the iterator and undestand. It is something as poiter for container. Listing of values works: IT1 + + indicates a memory which is followed
Your code is better but harder for me to understand him :D :D
you can think of myMap.end() as NULL so Line 1 above is saying that when I look for word in myMap, if the result is NULL, I didn't find it
so in that block you have in Line 2, I must initialize myMap[ word ] because right now, there is nothing there (not found). I initialize it by creating a Position instance and I add idx to the end of the vector inside this Position. By my Line 34, I initialize the value entry at myMap[ word ] by assigning my instance to it.
myMap[ word ] = p; // assign (copy) the value p to the key word in myMap
calls the assignment operator, but it actually copy-constructs p and puts it into myMap[word]
On my Line 36, it is simpler, because at myMap[word] (found), I already have a Position record there. Since it's there already, I can just add the new idx to the end of the vector:
myMap[ word ].m_positions.push_back( idx ); // add idx to the end of the vector
So I understand the code (good start). In the morning, I'll write a function to list the number of words. and function of the number of identical words
(We have 2 in the morning)
Ps: I do not know what wrong with the PM ...
How to modify this section to print only the words that occurred the first time? Then "if" I would use to list quantity
I'm trying it this way. When the input "mouse mouse car"
1 2 3 4
for ( it1=myMap.begin(); it1!=myMap.end(); ++it1 ) {
const string& key = it1->first;
const Position& value = it1->second;
for ( it2=value.m_positions.begin(); it2!=value.m_positions.end(); ++it2 )
It's hard for me to learn how to work with these functions. I wanted to solve the problem of entropy differently than binary tree. But I guess I'll have to finish the tree. I do not have much time (1 day: D)