| | |
Printing contents of a map containter
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2009
Posts: 136
Reputation:
Solved Threads: 0
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?
some of the error:
error: no match for âoperator=â in âpos =
C++ Syntax (Toggle Plain Text)
map<string, unsigned>::iterator pos; for(pos = wordList.begin(); pos != wordList.end(); ++pos) { cout << "Key: " << pos->first << endl; cout << "Value:" << pos->second << endl; }
some of the error:
error: no match for âoperator=â in âpos =
0
#2 27 Days Ago
I assume it's talking about the assignment of
Can you post more code so we can see more of what you're doing?
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.
•
•
Join Date: Feb 2009
Posts: 136
Reputation:
Solved Threads: 0
0
#3 27 Days Ago
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <map> #include <sstream> #include <fstream> #include <cstring> using namespace std; unsigned int read_words(map <string, unsigned>&); string remove_punctuation(const string&); void print_words(const map <string, unsigned>&, unsigned int); int main() { map<string, unsigned> wordList; unsigned int numwords; numwords = read_words(wordList); print_words(wordList, numwords); system ("pause"); return 0; } unsigned int read_words(map <string, unsigned>& wordList) { ifstream inFile; inFile.open("data5.txt"); if(inFile.fail()) { cout << "input file did not open"; exit(0); } string word; string newWord; unsigned int numCount = 0; while(inFile >> word) { newWord = remove_punctuation(word); if(newWord.length() > 0) { wordList[newWord]++; numCount++; } } return numCount; } string remove_punctuation(const string& word) { word.c_str(); string fixWord; fixWord.c_str(); int fixCount = 0; string temp; for(int i = 0; word[i] != NULL; ++i) { if(isalpha(word[i])) { fixWord[fixCount] = tolower(word[i]); ++fixCount; } } temp = fixWord; return temp; } void print_words(const map <string, unsigned>& wordList, unsigned int numwords) { cout << numwords; map<string, unsigned>::iterator pos; for(pos = wordList.begin(); pos != wordList.end(); ++pos) { cout << "Key: " << pos->first << endl; //cout << "Value:" << pos->second << endl; } }
0
#4 27 Days Ago
You're passing in a const map, so you've gotta use a const_iterator:
should work.
To explain. A
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.
![]() |
Similar Threads
- Printing contents of linked list (C)
- Printing array contents (Java)
- Erasing Map Iterators (C++)
- Simple Map Question (C++)
- Screen printing (Pascal and Delphi)
- Printing in wxWidgets (C++)
- Sorting a map<string,long> by the value... (C)
- windows media player opens at startup with error (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: Help me with this code
- Next Thread: Any function point specialists out there?
| Thread Tools | Search this Thread |
api array based binary bitmap 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 java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node 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 visualstudio win32 windows winsock word wordfrequency wxwidgets






