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)
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;
}
Get Answers For Free
Most questions answered within 1 hours.