| | |
counting a string array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 1
Reputation:
Solved Threads: 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!!!!
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!!!!
•
•
Join Date: Jan 2008
Posts: 119
Reputation:
Solved Threads: 10
C++ Syntax (Toggle Plain Text)
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;
At first you must load map contents (where is the code? ). But you have another problem:
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
Now get F1 help on map insertion (or google it) then send us the next version of the program product...
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.
•
•
Join Date: Jan 2008
Posts: 119
Reputation:
Solved Threads: 10
•
•
•
•
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 considermap<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...
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:
•
•
•
•
map<char*,int> is ok for he's requirements. He just has to count them, not have them in a sorted dictionary
•
•
•
•
i am trying to count the occurrences of different words in a string array.
•
•
Join Date: Jan 2008
Posts: 119
Reputation:
Solved Threads: 10
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...
![]() |
Similar Threads
- Counting numbers in an array (C++)
- Counting Array Entries (C)
- counting strings (C)
- Counting the amount of letters in a phrase! (C++)
- Need Help counting Array Length (C++)
Other Threads in the C++ Forum
- Previous Thread: need help! review for midterm!!!
- Next Thread: Help with Explicit parameter polymorhphism
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






