C'est la première fois que j'implémente une carte en C++. Donc, étant donné un tableau de caractères avec du texte, je veux compter la fréquence de chaque mot se produisant dans le texte. J'ai décidé d'implémenter la carte pour stocker les mots et comparer les mots suivants et incrémenter un compteur. Voici le code que j'ai écrit jusqu'à maintenant.Comptabiliser la fréquence des mots à l'aide de la carte
const char *kInputText = "\
So given a character array with text, I want to count the frequency of
each word occurring in the text.\n\
I decided to implement map to store the\n\
words and compare following words and increment a counter.\n";
typedef struct WordCounts
{
int wordcount;
}WordCounts;
typedef map<string, int> StoreMap;
//countWord function is to count the total number of words in the text.
void countWord(const char * text, WordCounts & outWordCounts)
{
outWordCounts.wordcount = 0;
size_t i;
if(isalpha(text[0]))
outWordCounts.wordcount++;
for(i=0;i<strlen(text);i++)
{
if((isalpha(text[i])) && (!isalpha(text[i-1])))
outWordCounts.wordcount++;
}
cout<<outWordCounts.wordcount;
}
//count_for_map() is to count the word frequency using map.
void count_for_map(const char *text, StoreMap & words)
{
string st;
while(text >> st)
words[st]++;
}
int main()
{
WordCounts wordCounts;
StoreMap w;
countWord(kInputText, wordCounts);
count_for_map(kInputText, w);
for(StoreMap::iterator p = w.begin();p != w.end();++p)
{
std::cout<<p->first<<"occurred" <<p->second<<"times. \n";
}
return 0;
}
Error: No match for 'operator >>' in 'text >> st'
I understand this is an operator overloading error, so I went ahead and
wrote the following lines of code.
//In the count_for_map()
/*istream & operator >> (istream & input,const char *text)
{
int i;
for(i=0;i<strlen(text);i++)
input >> text[i];
return input;
}*/
Am I implementing map in the wrong way?
Vous devez décrire probablement aussi votre erreur dans le texte principal au-dessus du code, puisque les lecteurs ont actuellement à passer au crible à travers votre code pour comprendre ce que le problème est que vous avez. – DWright