Actaeonis
Actaeonis

Reputation: 159

Writing the contents of a map through operator overloading

I'm trying to print different kinds of containers with the use of operator overloading. However when I want to print a map<int, stack<vector<int > > >it returns an empty string where the stack should be. When I try to print the stack seperately I do not have any problems.

#include <vector>
#include <stack>
#include <deque>
#include <set>
#include <iostream>
#include <map>
using namespace std;

typedef struct {
   string naam;
   int leeftijd;
}Persoon;

ostream& operator<<(ostream& out, const Persoon &p) {
   out<<"! "<<p.naam<<" "<<p.leeftijd<<" !";
   return out;
}
template<typename D, typename T>
ostream& operator<<(ostream&out, map<T,D> &m) {
   typename map<T,D>::iterator my_it = m.begin();
   out<<"[*] ";
   while(my_it != m.end()) {
      cout<<my_it->first<< " -> "<<my_it->second<<" ";
      my_it++;
   }
   out<<"[*]\n";
}
template<typename T>
ostream& operator<<(ostream& out, vector<T> &v){
   out<<"[";
   int i;
   for(i = 0; i < v.size()-1; i++) {
      out<<v[i]<<"|";   
   }
   out<<v[i]<<"]\n";
   return out;
}
template <typename T>
ostream& operator<<(ostream& out, stack<T> &s){
   out<<"{";
   while(!s.empty()) {
      out<<"|"<<s.top()<<"|";
      s.pop();  
   }
   out<<"}\n";
   return out;
}
int main() {
   vector<int> v(5,3);
   /*cout<<v;
     stack<string> s;
     s.push("Plate1");
     s.push("Plate2");
     s.push("Plate3");
     cout<<s;
     map<int, Persoon> m;
     Persoon Bill = {"Bill",59};
     Persoon Steven = {"Steven",79};
     Persoon Rob = {"Rob",26};

     m.insert(pair<int,Persoon>(59,Bill));
     m.insert(pair<int,Persoon>(79,Steven));
     m.insert(pair<int,Persoon>(26,Rob));
     cout<<m;
    */
   stack<vector<int> > s2;
   s2.push(v);
   s2.push(v);
   cout<<s2;

   map<int, stack<vector<int> > > m2;
   m2.insert(pair<int, stack<vector<int> > >(5,s2));
   cout<<m2;
}

Upvotes: 0

Views: 87

Answers (1)

Ixrec
Ixrec

Reputation: 976

Your operator<< for stack is popping all of the elements off of the stack, so the stack is empty by the time you make a copy of it to put in the map.

To demonstrate, if I change your main() to this:

    int main() {
        stack<int> s2;
        s2.push(1);
        s2.push(2);
        cout << s2.size() << '\n';
        cout << s2;
        cout << s2.size() << '\n';
    }

The output is:

2
{|2||1|}
0

As far as I can tell, popping is the only way to "iterate" over a stack's elements (even in C++11), so you probably have no choice but to make a copy of the stack inside your operator and pop off all of the copy's elements. This code seems to work for me:

    ostream& operator<<(ostream& out, stack<T> &s){
        stack<T> scopy = s;
        out<<"{";
        while(!scopy.empty()) {
            out<<"|"<<scopy.top()<<"|";
            scopy.pop();    
        }
        out<<"}\n";
        return out;
    }

Upvotes: 2

Related Questions