Counting frequency of words in a text file using STL containers

W

writeanand

How can I count the frequency of words in a ASCII File using STL?
a) I dont know what words will be found in the file
b) The max number of occurrences is 10,000 per word (in case that
matters)

Can anyone give me an example program and compare/contrast the use of
various STL containers for this problem?
 
T

Thomas Tutone

How can I count the frequency of words in a ASCII File using STL?
a) I dont know what words will be found in the file
b) The max number of occurrences is 10,000 per word (in case that
matters)


Use a std::map<std::string, int>. Look up std::map in your
coursebook, and it should be fairly obvious how to do this. If you
have trouble, post some code and explain the problem you're having.

Best regards,

Tom
 
W

writeanand

Tom,

THank you. When the word already exists in the map, then I can use freq
++ in the "int" part of the map.
My problem is, when I find a new word, there is no existing entry, so
I cant use (freq++) in the "int" part of the map.
Can u give an example for the above?

Thanks
 
D

David Harmon

On 8 Feb 2007 08:25:40 -0800 in comp.lang.c++, "(e-mail address removed)"
THank you. When the word already exists in the map, then I can use freq
++ in the "int" part of the map.
My problem is, when I find a new word, there is no existing entry, so
I cant use (freq++) in the "int" part of the map.

Why not? Just use operator[] and let it create a new entry.
 
W

writeanand

On 8 Feb 2007 08:25:40 -0800 in comp.lang.c++, "(e-mail address removed)"
THank you. When the word already exists in the map, then I can use freq
++ in the "int" part of the map.
My problem is, when I find a new word, there is no existing entry, so
I cant use (freq++) in the "int" part of the map.

Why not? Just use operator[] and let it create a new entry.

How Though? Sorry for my ignorance, I need a simple example since I am
new to this :(
 
T

Thomas Tutone

On 8 Feb 2007 08:25:40 -0800 in comp.lang.c++, "(e-mail address removed)"
Why not? Just use operator[] and let it create a new entry.

How Though? Sorry for my ignorance, I need a simple example since I am
new to this :(

Suppose you have a std::map<std::string, int> called wordCount.
Suppose the next word you have is stored in a std::string called
word. Suppose that word is currently equal to "the". Now, you want
to increment its count accordingly. Then just do this:

++wordCount[word];

This will work whether word's value previously existed in wordCount or
not.

If you have more problems, post them.

Best regards,

Tom
 
D

Daniel T.

On 8 Feb 2007 08:25:40 -0800 in comp.lang.c++, "(e-mail address removed)"
THank you. When the word already exists in the map, then I can use freq
++ in the "int" part of the map.
My problem is, when I find a new word, there is no existing entry, so
I cant use (freq++) in the "int" part of the map.

Why not? Just use operator[] and let it create a new entry.

How Though? Sorry for my ignorance, I need a simple example since I am
new to this :(

int main() {
map<string, int> freq;
++freq["hello"];
assert( freq["hello"] == 1 );
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top