问题描述:

i want to see the number of appearance of words from some phrases.

My problem is that i can't use map to do this:

map[word] = appearnce++;

Instead i have a class that uses binary tree and behaves like a map, but i only have the method:

void insert(string, int);

Is there a way to counts the words apperances using this function?(because i can't find a way to increment the int for every different word) Or do I have to overload operator [] for the class? What should i do ?

网友答案:

Presumably you also have a way to retrieve data from your map-like structure (storing data does little good unless you can also retrieve it). The obvious method would be to retrieve the current value, increment it, and store the result (or store 1 if retrieving showed the value wasn't present previously).

网友答案:

I guess this is homework and you're learning about binary trees. In that case I would implement operator[] to return a reference to the existing value (and if no value exists, default construct a value, insert it, and return that. Obviously operator[] will be implemented quite similarly to your insert method.

网友答案:

can you edit "insert" function? if you can, you can add static variable that count the appearnces inside the function

相关阅读:
Top