counting a string array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 1
Reputation: empathy is an unknown quantity at this point 
Solved Threads: 0
empathy empathy is offline Offline
Newbie Poster

counting a string array

 
0
  #1
Nov 3rd, 2008
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!!!!
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 29
Reputation: jbrock31 is an unknown quantity at this point 
Solved Threads: 1
jbrock31 jbrock31 is offline Offline
Light Poster

Re: counting a string array

 
0
  #2
Nov 3rd, 2008
I would recommend posting your code. You are not going to find anyone here that will give you answers without showing your effort.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 119
Reputation: kux is on a distinguished road 
Solved Threads: 10
kux kux is offline Offline
Junior Poster

Re: counting a string array

 
0
  #3
Nov 3rd, 2008
  1. char *stringarray[] = { "mama", "tata", "bunica", "mama", "tata", "georgel" };
  2. map< char*, int > mycountingmap;
  3.  
  4. for ( size_t i = 0; i < sizeof( stringarray ) / sizeof( char* ); ++i )
  5. ++mycountingmap[ stringarray[i] ] ;
  6.  
  7. //print result
  8. for ( size_t i = 0; i < sizeof( stringarray ) / sizeof( char* ); ++i )
  9. cout<<stringarray[i] <<" "<< mycountingmap[ stringarray[i] ] <<" times"<<endl;
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: counting a string array

 
0
  #4
Nov 3rd, 2008
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...
Last edited by ArkM; Nov 3rd, 2008 at 3:47 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 119
Reputation: kux is on a distinguished road 
Solved Threads: 10
kux kux is offline Offline
Junior Poster

Re: counting a string array

 
0
  #5
Nov 4th, 2008
Originally Posted by ArkM View Post
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: counting a string array

 
0
  #6
Nov 4th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 119
Reputation: kux is on a distinguished road 
Solved Threads: 10
kux kux is offline Offline
Junior Poster

Re: counting a string array

 
0
  #7
Nov 4th, 2008
Originally Posted by ArkM View Post
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...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC