0

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 ?

Adrian
  • 19,440
  • 34
  • 112
  • 219
  • 1
    What does `insert` do if the string is already there? – Jon Mar 09 '11 at 15:19
  • Is that value of `map[word]` supposed to be the number of appearances it makes? `map[word] += 1` should work unless you are not allowed to directly increment - sorry if I'm not understanding totally. – RageD Mar 09 '11 at 15:20
  • Only insert ? No delete ? No get ? if you get those two you can "get" "delete", "insert" value + 1. – J.N. Mar 09 '11 at 15:20
  • @J.N.: yes i have retrive too, i didnt think of that, my bad – Adrian Mar 09 '11 at 15:25
  • Why can't you use `std::map`? – Lightness Races in Orbit Mar 09 '11 at 15:25
  • @vBx: Maybe, you will get some hint from this solution : [Elegant ways to count occurrence of words](http://stackoverflow.com/questions/4888879/elegant-ways-to-count-words-in-a-file-in-c/4888916#4888916) – Nawaz Mar 09 '11 at 15:30
  • @Tomalak Geret'kal : thats how to project sounds...so its to bad,it takes lot of time to implement a class acting like a map, and its frustrating when you know you have a map at your disposal an cant use it – Adrian Mar 09 '11 at 15:32
  • @ Nawaz : thanks, but all of them it i saw corectly are using map :) I remeber how easy it was in python to do this, it is easy in c++ to if you can use map:) – Adrian Mar 09 '11 at 15:35
  • 1
    This question is unclear. What is the definition of insert? What is the declaration and implementation of map? If this is homework tag it as such and state the assignment before asking specific questions with _relevant_ code examples of what you've tried. – AJG85 Mar 09 '11 at 15:35
  • @vBx: Maybe I misunderstood but I don't think that answered my question. Why can't you use `std::map`? – Lightness Races in Orbit Mar 09 '11 at 15:38
  • The project explicty says: not to use map.... thats why i cant used it :) – Adrian Mar 09 '11 at 16:32

3 Answers3

2

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).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
1

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.

Kevin
  • 24,871
  • 19
  • 102
  • 158
  • yes, its homework, but its not my homework, doesnt matter, its still homework, yes, your solution is a way too, thanks. – Adrian Mar 09 '11 at 15:26
1

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

Aviv A.
  • 697
  • 5
  • 11