I am doing a program on files.The program should read a file like example.txt and find the number of occurrences of a word.for example say
Example.txt contains :
apple apple sugar meat milk groundnut milk
output :
name number of occurrences
apple 2
sugar 1
meat 2
...
I have done the program like this :
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
int main()
{
ifstream myfile2("example.txt");
vector<string> data;
vector<string>::const_iterator iter;
you should better use map to store data.
map<string, int count> You should read each word from the file not the line
whenever you extract a word try to find in the map
if found then increment the count
else insert the entry with count as 1.
So at the end when you will traverse the map you can exact number of words occurances in file.