0

hi

i am trying to count the occurrences of different words in a string array.

while(end of array)
{
while(end of array)
{
compare ith element with all other elements
inc counter
}
increment the element number
}

but i guess something is terribly wrong here as i am not able to get the required result. can someone point me out where i could have gone wrong!!! thanks!!!!

4
Contributors
6
Replies
7
Views
8 Years
Discussion Span
Last Post by kux
0

I would recommend posting your code. You are not going to find anyone here that will give you answers without showing your effort.

0
char *stringarray[] = { "mama", "tata", "bunica", "mama", "tata", "georgel" };
	map< char*, int > mycountingmap; 
	
	for ( size_t i = 0; i < sizeof( stringarray ) / sizeof( char* ); ++i )
		++mycountingmap[ stringarray[i] ] ;

       //print result
       for ( size_t i = 0; i <  sizeof( stringarray ) / sizeof( char* ); ++i )
		cout<<stringarray[i] <<" "<< mycountingmap[ stringarray[i] ] <<" times"<<endl;
0

At first you must load map contents (where is the code? ). But you have another problem: std::map<char*,int> can't help you because its key_type is char* but operator< for char* type compares pointer values, not referred C-strings. Therefore you will get wrong dictionary...

It's possible to define a simple wrapper class for char* type with its own operator< but it seems you can't do it now. So consider map<string,int> type for your dictionary: it's less effective but a very simple solution.

Now get F1 help on map insertion (or google it) then send us the next version of the program product...

0

At first you must load map contents (where is the code? ). But you have another problem: std::map<char*,int> can't help you because its key_type is char* but operator< for char* type compares pointer values, not referred C-strings. Therefore you will get wrong dictionary...

It's possible to define a simple wrapper class for char* type with its own operator< but it seems you can't do it now. So consider map<string,int> type for your dictionary: it's less effective but a very simple solution.

Now get F1 help on map insertion (or google it) then send us the next version of the program product...

map<char*,int> is ok for he's requirements. He just has to count them, not have them in a sorted dictionary

0

To kux

map<char*,int> is ok for he's requirements. He just has to count them, not have them in a sorted dictionary

Alas, all four text literals "mama" (for example) have different addresses (as usually). So map<char*,int> contains four different elements for the word "mama"! Obviously, it's not ok for his/her requirements. Read the original post carefully:

i am trying to count the occurrences of different words in a string array.

;)

0

To kux

Alas, all four text literals "mama" (for example) have different addresses (as usually). So map<char*,int> contains four different elements for the word "mama"! Obviously, it's not ok for his/her requirements. Read the original post carefully:

;)

hmm, i think you are wrong here. I mean i'm shure you are wrong here. On my STL implementation std::map has a template specialization that for char* keys uses the char* content to identify keys, not the address. Just try it. It will corectly count the words...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.