text: Q:
  
  How to print out an iterator for a set of sets
  
  I have made a code that should construct a set of sets. However, when I use an iterator to try to print out the set obtained, I run into an error. 
  This is my code
  #include <iostream>
  #include <set>
  using namespace std;
  
  int main()
  {
      set<long> A, B, C;
  
      for (long k=1; k<4; k++) A.insert(k);   // A={1, 2, 3}
      for (long k=4; k<6; k++) B.insert(k);   // B={4, 5}
      C.insert(6);                            // C={6}
  
      set< set<long> > S;       // Now implement S as a set of  finite sets
      S.insert(A);
      S.insert(B);
      S.insert(C);
  
      /** Finally, print out S using an iterator si: **/
  
      set< set<long> >::const_iterator si;
  
      for (si=S.begin(); si !=S.end(); si)
      {
          cout << *si << " ";
      }
      cout << endl;
  
      return 0;
  }
  
  If I comment the line starting with cout,  the program builds without error. However, there is an error on this line because if I try to build the computer refuses it and I get the following error message: error: no match for 'operator<<' in 'std::cout << si.std::_Rb_tree_const_iterator<_Tp>::operator* >()'|
  Now I am pretty new to this, but I suspect if the error has something to do with the fact that I should redefine the operator "<<"? Or what?
  What should I do to fix the code and print out the set?
  Thanks for your help.
  
  A:
  
  You need to loop through the inner set:
  #include <set>
  #include <iostream>
  using namespace std;
  
  int main()
  {
  set<long> A, B, C;
  
  for (long k = 1; k<4; k++) A.insert(k);   // A={1, 2, 3}
  for (long k = 4; k<6; k++) B.insert(k);   // B={4, 5}
  C.insert(6);                            // C={6}
  
  set< set<long> > S;       // Now implement S as a set of  finite sets
  S.insert(A); S.insert(B); S.insert(C);
  
  /** Finally, print out S using an iterator si: **/
  
  set< set<long> >::const_iterator si;
  set<long>::const_iterator si2;
  
  for (si = S.begin(); si != S.end(); ++si)
  {
      for (si2 = si->begin(); si2 != si->end(); ++si2)
      {
          cout << *si2 << " ";
      }
      cout << endl;
  }
  cout << endl;
  
  return 0;
  }
  
  The C++ 11 11 answer is more elegant:
  #include <set>
  #include <iostream>
  using namespace std;
  
  int main()
  {
  set<long> A, B, C;
  
  for (long k = 1; k<4; k++) A.insert(k);   // A={1, 2, 3}
  for (long k = 4; k<6; k++) B.insert(k);   // B={4, 5}
  C.insert(6);                            // C={6}
  
  set< set<long> > S;       // Now implement S as a set of  finite sets
  S.insert(A); S.insert(B); S.insert(C);
  
  /** Finally, print out S using an iterator si: **/
  
  for (auto si : S)
  {
      for (auto si2 : si)
      {
          cout << si2 << " ";
      }
      cout << endl;
  }
  cout << endl;
  
  return 0;
  }
  
  