Search code examples
c++11dictionaryoperator-overloadingostreamistream

ostream operator overload for struct with map


I have created a grades struct for students and am attempting to overload '<<' operator.

// Sample output:
a12345678
2             //number of elements in map
COMP3512 87  
COMP3760 68

struct Grades {
  string             id;       // student ID, e.g,, a12345678
  map<string, int> scores;     // course, score, e.g. COMP3512, 86
};

I have previously overloaded the operator<< to obtain the information independently.

ostream& operator<<(ostream& os, const Grades g) { 
  return os << g.id << '\n' ... 

  // return os << g.id << '\n' << g.scores;   produces an error
}

I suspect it has something to do with the overload not having correct syntax for a map, like the one below.

ostream& operator<<(ostream& os, const map<string, int>& s) {
  for (auto it = s.begin(); it != s.end(); ++it) 
    os << (*it).first << ' ' << (*it).second << endl;

  return os;
}

Is there a way to produce the sample output through one overload, or do I require both of the current implementations: one for the map:grades.scores and the other one for the string: grades.id

Thanks for the assistance.


Solution

  • It seems weird that you weren't able to sort it out yourself, since if I understood the question correctly you just need to merge the two overloads into one so you iterate the map from the const Grades g like so:

    #include <iostream>
    #include <map>
    #include <string>
    
    using namespace std;
    
    struct Grades {
        string             id;       // student ID, e.g,, a12345678
        map<string, int> scores;     // course, score, e.g. COMP3512, 86
    };
    
    
    ostream& operator<<(ostream& os, const Grades g) { 
        os << g.id << endl << g.scores.size() << endl;
        for (auto it = g.scores.begin(); it != g.scores.end(); ++it) 
            os << (*it).first << ' ' << (*it).second << endl;
        return os;
    }
    
    int main(int argc, char** argv)
    {
        Grades g;
        g.id = "a12345678";
        g.scores["COMP3512"] = 87;
        g.scores["COMP3760 "] = 68;
        cout << g;
        return 0;
    }