#include <iostream>
#include <map>
#include <sstream>
#include <fstream>
int main() {
//this map maps each word in the file to their respective count
std::map<std::string, int> stringCount;
std::string word, line;
int count = 0;//this count the total number of words
std::ifstream inputFile("input.txt");
if(inputFile)
{
while(std::getline(inputFile, line))//go line by line
{
std::istringstream ss(line);
while(ss >> word)//go word by word
{
//increment the count
stringCount[word]++;
}
}
}
else
{
std::cout<<"File cannot be opened"<<std::endl;
}
inputFile.close();
std::cout<<"Total number of unique words are:"<<stringCount.size()<<std::endl;
for(std::pair<std::string, int> pairElement: stringCount)
{
std::cout<<pairElement.first<<" : "<<pairElement.second<<std::endl;
}
return 0;
}
this is the first line
second line is this
third line again and again