Reputation: 21
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;"
Upvotes: 0
Views: 770
Reputation: 3877
Use an std::multiset
or std::unordered_multiset
. The performance depends a bit on your data set so some tuning is required to find the best one in practice. Something like this would work (adapt with your file reading code):
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_multiset<string> dict;
for (auto&& s : {"word1", "word2", "word1"}) {
dict.insert(s);
}
std::cout << dict.count("word1") << std::endl; // prints 2
return 0;
}
Depending on the data set & size, you could also use a more optimised data structure for storing & comparing strings, such as a trie, but this is not available in the standard, or boost
and most of the times is a bit of an overkill IMHO (although you can find some implementations).
Upvotes: 1
Reputation: 6727
ifstream inputFile("data.txt");
if(!inputFile.is_open()) {
cerr << "Can't open data.txt\n";
exit(0);
}
map<string, int> freq;
while(getline(inputFile, word, ' '))
++freq[word];
Upvotes: 1