944,149 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1426
  • C++ RSS
Oct 28th, 2009
0

Printing contents of a map containter

Expand Post »
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?

C++ Syntax (Toggle Plain Text)
  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 =
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
lancevo3 is offline Offline
146 posts
since Feb 2009
Oct 29th, 2009
0
Re: Printing contents of a map containter
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; Oct 29th, 2009 at 4:14 am.
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Oct 29th, 2009
0
Re: Printing contents of a map containter
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Junior Poster
lancevo3 is offline Offline
146 posts
since Feb 2009
Oct 29th, 2009
0
Re: Printing contents of a map containter
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; Oct 29th, 2009 at 5:21 pm.
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007

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: Help me with this code
Next Thread in C++ Forum Timeline: class question





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


Follow us on Twitter


© 2011 DaniWeb® LLC