title: How to use setw with objects
body: <p>I'm trying to format output using the NTL library (a number theory library). One of the objects is the GF2X object, which is a polynomial represented as a string of coefficients. A quick example:</p>
  
  <pre><code>GF2X a = GF2X(5,1);
  a++;
  cout&lt;&lt;a;
  </code></pre>
  
  <p>will yield <code>[1 0 0 0 0 1]</code> which is the same as x^5 + 1. My question is about formatting this output using setw. I want to be able to output various length GF2X objects, prepended by a number, and appended with a string. I'd like my output to look like the following:</p>
  
  <pre><code>  1:    [x x x x x x x x]       string here
   15:    [x x x]                 string here
  </code></pre>
  
  <p>I would also settle for the right ] to be aligned, which is what I should probably expect if I'm using setw. However, when i use the code (variable names ommitted for simplicity):</p>
  
  <pre><code>    cout &lt;&lt; setw(3)&lt;&lt; int &lt;&lt;": "&lt;&lt; setw(35) &lt;&lt; GF2X &lt;&lt; setw(15) &lt;&lt; string &lt;&lt; endl;
  </code></pre>
  
  <p>I get output more like this (some white space removed for compactness)</p>
  
  <pre><code>  1:            [x x x x x x x x]   string here
   15:            [x x x]   string here
  </code></pre>
  
  <p>In other words, the setw function seems to be treating the entire output of <code>&lt;&lt;GF2X</code> as a single character, and doesn't seem to actually account for the size of the output string. As you can see from the output I've shown you, the left side of the GF2X output is aligned, but the right side isn't, whereas typically, setw seems to align the right side of outputs.</p>