开发者

Problem counting words from a phrase without using std::map

开发者 https://www.devze.com 2023-02-15 15:38 出处:网络
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:

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开发者_如何学Python, 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

0

精彩评论

暂无评论...
验证码 换一张
取 消