473,545 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Hash Map

1 New Member
Hi,i have the following code to find the frequences of same words in a file.i used hashmap for this purpose but when i run the code, it enters a infinite loop.when i debug the code,i see that the iter variable is not incremented at the end of each loop.Can anyone help me with this problem?Thanks a lot.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include<fstream.h>
  3. #include <ext/hash_map>
  4. #include <string.h>
  5. #define HASH __gnu_cxx
  6. using namespace std;
  7. using std::string;
  8.  
  9. HASH::hash_map <const char*,int> roots;
  10.  
  11. int main() {
  12.  
  13.     string line;
  14.     char * lineChar;
  15.     int word_freq;
  16.  
  17.     fstream rootCorpusFile("deneme.txt",ios::in);
  18.     if(rootCorpusFile.is_open()){
  19.         while(!rootCorpusFile.eof()){
  20.  
  21.             getline(rootCorpusFile,line);
  22.             cout<<"Line is "<<line<<"\n";
  23.              lineChar=(char*)line.c_str();           
  24.  
  25.              cout<<"LineChar is "<<lineChar<<"\n";
  26.             if((word_freq=roots[lineChar])!=NULL){
  27.                 cout<<"Word "<<lineChar<<" Freq "<<word_freq;
  28.                 word_freq++;
  29.                 roots[lineChar]=word_freq;
  30.             }
  31.             else{
  32.                 roots[lineChar]=1;
  33.             }
  34.         }
  35.  
  36.     }
  37.     else{
  38.         cout<<"File can not be openned!/n";
  39.     }
  40.  
  41.  
  42. HASH::hash_map <const char*,int>::iterator iter;
  43.  
  44. for (iter = roots.begin(); iter != roots.end(); iter++) {
  45.     std::string key, value;
  46.     key = iter->first;
  47.     value = iter->second;
  48.     cout<<"Key "<<key<<" Value"<<value<<"\n";
  49.  
  50. }
  51.  
  52.     rootCorpusFile.close();
  53.     return 0;
  54. }
  55.  
  56.  
  57.  
  58. ilknur
Oct 18 '08 #1
0 1200

Sign in to post your reply or Sign up for a free account.

Similar topics

3
4152
by: Murali | last post by:
I have a requirement where I have to use two unsigned ints as a key in a STL hash map. A couple of ways to do this is 1. create a struct with two unsigned ints and use that as key (write my own HashFcn and EqualKey template args) or, 2. convert the two unsigned ints to char*s, concatenate them and use that as Key. For method 1, the...
2
3764
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to produce two messages having the same message digest. That conjecture is false, as demonstrated by Wang, Feng, Lai and Yu in 2004 . Just recently,...
24
4273
by: kdotsky | last post by:
Hello, I am using some very large dictionaries with keys that are long strings (urls). For a large dictionary these keys start to take up a significant amount of memory. I do not need access to these keys -- I only need to be able to retrieve the value associated with a certain key, so I do not want to have the keys stored in memory. Could...
12
6983
by: Arash Partow | last post by:
Hi all, I've ported various hash functions to python if anyone is interested: def RSHash(key): a = 378551 b = 63689 hash = 0
21
3173
by: Johan Tibell | last post by:
I would be grateful if someone had a minute or two to review my hash table implementation. It's not yet commented but hopefully it's short and idiomatic enough to be readable. Some of the code (i.e. the get_hash function) is borrowed from various snippets I found on the net. Thee free function could probably need some love. I have been...
21
3858
by: Hallvard B Furuseth | last post by:
Is the code below valid? Generally a value must be accessed through the same type it was stored as, but there is an exception for data stored through a character type. I'm not sure if that applies in this case though: #include <limits.h> unsigned foo(void) { static const union { unsigned char str;
139
14055
by: ravi | last post by:
Hi can anybody tell me that which ds will be best suited to implement a hash table in C/C++ thanx. in advanced
18
1794
by: beginner | last post by:
Hi All. I'd like to do the following in more succint code: if k in b: a=b else: a={} b=a
5
8158
by: Jeff | last post by:
Lets say we have what I would call a "hash": var HASH =new Array(); HASH='first'; HASH='second'; HASH='third'; I'd like to return that as JSON data. Is there a direct way to do that?
1
2960
by: sixtyfootersdude | last post by:
Good Morning! I am a perl newbie and I think that I am struggling with references. I have an array of references to hashes which I am trying to print. This is what I have: for(my $i=0; $i<@input; $i++){ my $hash = $input; print "$i: \n";
0
7475
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7664
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7921
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7771
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5982
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4958
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3465
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1023
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.