473,399 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Counting frequency of words in a text file using STL containers

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?

Feb 8 '07 #1
8 4279
On Feb 8, 11:14 am, "writean...@gmail.com" <ekr...@gmail.comwrote:
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
Feb 8 '07 #2
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
On Feb 8, 9:18 am, "Thomas Tutone" <Thomas8675...@yahoo.comwrote:
On Feb 8, 11:14 am, "writean...@gmail.com" <ekr...@gmail.comwrote:
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

Feb 8 '07 #3
On 8 Feb 2007 08:25:40 -0800 in comp.lang.c++, "wr********@gmail.com"
<ek****@gmail.comwrote,
>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.
Feb 8 '07 #4
On Feb 8, 10:05 am, David Harmon <sou...@netcom.comwrote:
On 8 Feb 2007 08:25:40 -0800 in comp.lang.c++, "writean...@gmail.com"
<ekr...@gmail.comwrote,
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 :(

Feb 8 '07 #5
On Feb 8, 12:10 pm, "writean...@gmail.com" <ekr...@gmail.comwrote:
On Feb 8, 10:05 am, David Harmon <sou...@netcom.comwrote:
On 8 Feb 2007 08:25:40 -0800 in comp.lang.c++, "writean...@gmail.com"
<ekr...@gmail.comwrote,
>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 :(
Suppose you have a std::map<std::string, intcalled 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

Feb 8 '07 #6
In article <11**********************@a75g2000cwd.googlegroups .com>,
"wr********@gmail.com" <ek****@gmail.comwrote:
On Feb 8, 10:05 am, David Harmon <sou...@netcom.comwrote:
On 8 Feb 2007 08:25:40 -0800 in comp.lang.c++, "writean...@gmail.com"
<ekr...@gmail.comwrote,
>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, intfreq;
++freq["hello"];
assert( freq["hello"] == 1 );
}
Feb 8 '07 #7
wr********@gmail.com wrote:
Tom,
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>
Feb 8 '07 #8
On Feb 8, 3:01 pm, "Default User" <defaultuse...@yahoo.comwrote:
writean...@gmail.com wrote:
Tom,

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>
*plonk*

Feb 8 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Simon Verona | last post by:
Not sure if this is the best group... it may be better off in one of the ADO groups, but I'm sure somebody here knows the answer: I'm trying to load up a text file using ADO.net, as follows: ...
2
by: Praveen_db2 | last post by:
Hi all Db2 8.1.3 windows Is there any way to write data into a text file using a stored procedure? The way we return a cursor output to the calling application, can we return data in a text...
2
by: asenthil | last post by:
Hai, i'm a beginner to java... just now i had tried to read and write files using java... and then i had tried to connect a database using jdbc... now i want to export the data's from a...
4
by: varsha desai | last post by:
Hello there, I want to change some data(which is in one line only) of text file using VB 6.0. Which is the best method for it? Another question is I want to delete last two, three lines...
1
by: neveen | last post by:
i want to open and read text file using j2me that can run on mobile 6630 then i want to make button called read that when pressed the data inside text display
2
by: charlesbritto | last post by:
A C++ program for counting individual string in a text file using file handling functions., for ex: if the text file contains, am a boy,am studying +2,am from chennai Now the result shoud...
2
by: sajidali | last post by:
i am trying to append a text file using c#. when i executes my application i writes to the text file using more then one functions. i want to append file only during execution of programe. next time...
4
by: kshw | last post by:
Hi, I'm trying to remove non-stop words from a text file using regular expresions but it is not working. I used something like ('^?or') in order to avoid removing (or) from the mibble of words...
0
by: Thiem Teddy | last post by:
Dear all I use VB6 to export data into xls or text file, I followed this example ( http://bytes.com/topic/visual-basic/answers/530866-how-export-table-data-into-xls-text-file-using-vb6 ). but I...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.