Declare a struct TokenFreq that consists of two data members
Declare a struct TokenFreq that consists of two data members: (1) string value; and (2) int freq; Obviously, an object of this struct will be used to store a specific token and its frequency. For example, the following object word stores the token \"dream\" and its frequency 100:
TokenFreq word;
word.value=\"dream\";
word.freq=100;
Implement the function vector getTokenFreq( string inFile_name); This function reads the specified input file line by line, identifies all the unique tokens in the file and the frequency of each token. It stores all the identified (token, freq) pairs in a vector and returns this vector to the calling function. (case insensitive)
Solution
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
struct TokenFreq{
string value;
int freq;
};
vector<TokenFreq> getTokenFreq(string inFile_name){
vector<TokenFreq> output;
ifstream inFile(inFile_name.c_str());
string temp;
while(inFile >> temp){
int index = -1;
for(int i = 0; i < output.size(); i++){
if(output[i].value.compare(temp) == 0){
index = i;
break;
}
}
if(index == -1){
TokenFreq t = {temp, 1};
output.push_back(t);
}
else{
output[index].freq++;
}
}
return output;
}
int main(){
string fileName = \"input.txt\";
vector<TokenFreq> result = getTokenFreq(fileName);
for(int i = 0; i < result.size(); i++){
cout << result[i].value << \" - \" << result[i].freq << \"\ \";
}
}
