I have a structure:
struct wordItem
{
string word;
int count;
};
I'm reading in a text file with many different words and storing them into an array.
ifstream inputFile("data.txt");
if(inputFile.is_open())
{
while(getline(inputFile, data, ' '))
{
wordItemList[i].word = data;
i++;
}
}
My question is what is the best way to count each time a word appears in the array. For example if my data.txt file was
the fox jumped over the fence
I want to be able to store how many times each word appears within the struct within the "int count;"
std::map<std::string, int>orstd::unordered_map<std::string, int>.unordered_maptomapif you want ordering). Yes, it really is that simple. In short, you don't really need that structure; the map will hold the count for your as the mapped-to value.