943,846 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1638
  • C++ RSS
Nov 3rd, 2008
0

counting a string array

Expand Post »
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!!!!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
empathy is offline Offline
1 posts
since Nov 2008
Nov 3rd, 2008
0

Re: counting a string array

I would recommend posting your code. You are not going to find anyone here that will give you answers without showing your effort.
Reputation Points: 22
Solved Threads: 14
Junior Poster in Training
jbrock31 is offline Offline
73 posts
since Oct 2008
Nov 3rd, 2008
0

Re: counting a string array

C++ Syntax (Toggle Plain Text)
  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;
kux
Reputation Points: 66
Solved Threads: 11
Junior Poster
kux is offline Offline
119 posts
since Jan 2008
Nov 3rd, 2008
0

Re: counting a string array

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.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Nov 4th, 2008
0

Re: counting a string array

Click to Expand / Collapse  Quote originally posted by ArkM ...
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
kux
Reputation Points: 66
Solved Threads: 11
Junior Poster
kux is offline Offline
119 posts
since Jan 2008
Nov 4th, 2008
0

Re: counting a string array

To kux
Quote ...
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:
Quote ...
i am trying to count the occurrences of different words in a string array.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Nov 4th, 2008
0

Re: counting a string array

Click to Expand / Collapse  Quote originally posted by ArkM ...
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...
kux
Reputation Points: 66
Solved Threads: 11
Junior Poster
kux is offline Offline
119 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: need help! review for midterm!!!
Next Thread in C++ Forum Timeline: Help with Explicit parameter polymorhphism





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC