fork download
  1. #include <vector>
  2. #include <iostream>
  3. #include <string>
  4. #include <map>
  5.  
  6. using namespace std;
  7.  
  8. class document
  9. {
  10. public:
  11. void missingMap(string, int);
  12. void displayMap() const; // this should be const
  13.  
  14. private:
  15. map<string, vector<int>> misspelled;
  16. };
  17.  
  18.  
  19.  
  20. void document::missingMap(string word, int lineNum)
  21. {
  22. misspelled[word].push_back(lineNum);
  23. }
  24.  
  25. void document::displayMap() const // this should be const
  26. {
  27. typedef map<string, vector<int>>::const_iterator map_iter;
  28. typedef vector<int>::const_iterator vec_iter;
  29.  
  30. for ( map_iter i = misspelled.begin(); i!= misspelled.end(); ++i )
  31. {
  32. cout << i->first << ": ";
  33. for (vec_iter j = i->second.begin(); j != i->second.end(); ++j)
  34. {
  35. cout << *j << ' ' /* << endl */; // Don't end the line here.
  36. }
  37.  
  38. cout << '\n'; // end the line here.
  39. }
  40. }
  41.  
  42. int main()
  43. {
  44. document doc;
  45. doc.missingMap("debugging", 1);
  46. doc.missingMap("process", 2);
  47. doc.missingMap("removing", 2);
  48. doc.missingMap("programming", 3);
  49. doc.missingMap("process", 4);
  50. doc.missingMap("putting", 4);
  51.  
  52. doc.displayMap();
  53. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
debugging: 1 
process: 2 4 
programming: 3 
putting: 4 
removing: 2