Printing contents of a map containter

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

Join Date: Feb 2009
Posts: 136
Reputation: lancevo3 is an unknown quantity at this point 
Solved Threads: 0
lancevo3 lancevo3 is offline Offline
Junior Poster

Printing contents of a map containter

 
0
  #1
28 Days Ago
Hey guys I am trying to print the contents of a map container I have. I seem to be getting a nasty error from the compiler which seems to come from the pos = wordList.begin() part. any suggestions?

  1. map<string, unsigned>::iterator pos;
  2. for(pos = wordList.begin(); pos != wordList.end(); ++pos)
  3. {
  4. cout << "Key: " << pos->first << endl;
  5. cout << "Value:" << pos->second << endl;
  6. }

some of the error:
error: no match for âoperator=â in âpos =
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,859
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 55
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso
 
0
  #2
27 Days Ago
I assume it's talking about the assignment of pos=wordlist.begin(). If so you'd imagine that wordlist isn't of type map<string,unsigned> from what the error says.

Can you post more code so we can see more of what you're doing?
Last edited by twomers; 27 Days Ago at 4:14 am.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 136
Reputation: lancevo3 is an unknown quantity at this point 
Solved Threads: 0
lancevo3 lancevo3 is offline Offline
Junior Poster
 
0
  #3
27 Days Ago
  1. #include <iostream>
  2. #include <map>
  3. #include <sstream>
  4. #include <fstream>
  5. #include <cstring>
  6.  
  7. using namespace std;
  8.  
  9. unsigned int read_words(map <string, unsigned>&);
  10. string remove_punctuation(const string&);
  11. void print_words(const map <string, unsigned>&, unsigned int);
  12.  
  13. int main()
  14. {
  15. map<string, unsigned> wordList;
  16.  
  17. unsigned int numwords;
  18.  
  19. numwords = read_words(wordList);
  20.  
  21. print_words(wordList, numwords);
  22.  
  23. system ("pause");
  24. return 0;
  25. }
  26.  
  27. unsigned int read_words(map <string, unsigned>& wordList)
  28. {
  29. ifstream inFile;
  30. inFile.open("data5.txt");
  31.  
  32. if(inFile.fail())
  33. {
  34. cout << "input file did not open";
  35. exit(0);
  36. }
  37.  
  38. string word;
  39. string newWord;
  40. unsigned int numCount = 0;
  41.  
  42. while(inFile >> word)
  43. {
  44. newWord = remove_punctuation(word);
  45. if(newWord.length() > 0)
  46. {
  47. wordList[newWord]++;
  48. numCount++;
  49. }
  50. }
  51. return numCount;
  52. }
  53.  
  54. string remove_punctuation(const string& word)
  55. {
  56. word.c_str();
  57. string fixWord;
  58. fixWord.c_str();
  59. int fixCount = 0;
  60. string temp;
  61.  
  62. for(int i = 0; word[i] != NULL; ++i)
  63. {
  64. if(isalpha(word[i]))
  65. {
  66. fixWord[fixCount] = tolower(word[i]);
  67. ++fixCount;
  68. }
  69. }
  70. temp = fixWord;
  71. return temp;
  72. }
  73.  
  74. void print_words(const map <string, unsigned>& wordList, unsigned int numwords)
  75. {
  76. cout << numwords;
  77.  
  78. map<string, unsigned>::iterator pos;
  79. for(pos = wordList.begin(); pos != wordList.end(); ++pos)
  80. {
  81. cout << "Key: " << pos->first << endl;
  82. //cout << "Value:" << pos->second << endl;
  83. }
  84. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,859
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 55
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso
 
0
  #4
27 Days Ago
You're passing in a const map, so you've gotta use a const_iterator:
map<string,unsigned>::const_iterator pos;
should work.

To explain. A const_iterator doesn't allow you modify anything in the map, while you may with iterators.
Last edited by twomers; 27 Days Ago at 5:21 pm.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC