Question

Create a function called statistics, this functions reads the accompanying text file and display each word...

Create a function called statistics, this functions reads the accompanying text file and display each word in the file along with the number of times the word appears. Must use the Map in C++ Standard Template Library (STL)

Homework Answers

Answer #1

There goes the code I have implemented it using a map and set in CPP stl.

Map is used to count the number of times the word occurs.

And set is used to uniquely insert text in it.

#include <iostream>
#include <fstream>
#include <set>
#include <map>
using namespace std;
void statistics(){
string text;
int count = 0;
set <string> s1;
map<string,int> my_map;
ifstream my_input_file;
my_input_file.open("sample.txt");
while (getline (my_input_file, text)){
my_map.insert(text,count);
s1.insert(text);
count++;
}
for (itr = s1.begin(); itr != s1.end(); ++itr)
{
cout << '\t' << *itr <<" " <<my_map[*itr];
}
cout << endl;
}
int main (){
return 0;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
c# please Create a “Main” program which reads a text file called “collectionOfWords.txt”. Include exception handling...
c# please Create a “Main” program which reads a text file called “collectionOfWords.txt”. Include exception handling to check if the file exists before attempting to open it. Read and print each string to the console. Next modify each word such at the first letter in each word is uppercase and all other letters are lowercase. Display the modified word on the console. Creating a text file: Open up Notepad and type in the following words. Save the file as collectionOfWords.txt...
Read the words in from the binary file and figure out how many times each word...
Read the words in from the binary file and figure out how many times each word appears in the file. Display the results to the user. Use ObjectInputStream to read binary file Use a HashMap with the word as a key (String) and an Integer as the value. For each word, first check to see if it already exists in the Map. If not, add the word as key with a value of 1 for the Integer value. If it...
How can we count how many times each word appears in a GB text file?
How can we count how many times each word appears in a GB text file?
Create a program that generates a file of random numbers, and then prints them in neat...
Create a program that generates a file of random numbers, and then prints them in neat fashion to another file, and also saves to that file the average and standard deviation of those numbers. I) First, you would need to generate a file of random numbers that consists of N random numbers (100 < N < 1000). Each random digit should be a real number (type double) between 0 and 50. This file and its digits would now serve as...
Write a python program that will perform text analysis on an input text using all of...
Write a python program that will perform text analysis on an input text using all of the following steps: 1. Take the name of an input file as a command line argument. For example, your program might be called using python3 freq.py example1 to direct you to process a plain text file called example. More information is given on how to do this below. 2. Read the contents of the file into your program and divide the text into a...
JAVA: Derive following statistics from some text file: number of words that are size 6 or...
JAVA: Derive following statistics from some text file: number of words that are size 6 or more, number of words used only once, number of words used such that each word is only counted once!! Use ArrayList and HashSet.
Description The word bank system maintains all words in a text file named words.txt. Each line...
Description The word bank system maintains all words in a text file named words.txt. Each line in the text file stores a word while all words are kept in an ascending order. You may assume that the word length is less than 20. The system should support the following three functions: Word lookup: to check whether a given word exists in the word bank. Word insertion: to insert a new word into the word bank. No insertion should be made...
Create a function called, convert. This function receives a string parameter called word which only contains...
Create a function called, convert. This function receives a string parameter called word which only contains digits (the string represents a positive number) and returns a list of numbers. This is how the function works: - This function calculates the number of times each digit has repeated in the input string and then generates a number based on that using the following formula and adds it to a list. For instance, if the digit x has been repeated n times,...
1.      Create 100 text files automatically; in each file write a random number from 1 to 10....
1.      Create 100 text files automatically; in each file write a random number from 1 to 10. Use outputstreams (fileoutputstream, buffredwriter….) 2.      Read the content of the 100 files and combine them into a 1 single file. 3.      Write java code to do the following: a.      To write the following text into a text file EEEESAAA@23SDCFSAWERF%WASDFGHWERTRQW b.      Read the file using a java program c.      Find how many D’s in the file d.      Extract the text between the @ and the # 1. Create 100 text files...
Using C programming Create a function called printMenu( ) with the following properties: Has no function...
Using C programming Create a function called printMenu( ) with the following properties: Has no function inputs or output. Prints the following menu to the screen: 1. Enter user name. 2. Enter scores. 3. Display average score. 4. Display summary. 5. Quit Create a function called printLine( ) with the following properties: Takes as input a char Takes as input an integer corresponding to the number of times to print the character Has no function output. For example, if we...