title: How do I iterate `std::stack` elements in a for loop?
body: <p>I'm writing a FIR filter that is supposed to calculate <a href="https://en.wikipedia.org/wiki/Moving_average" rel="noreferrer">running average</a> of an input sequence.</p>
  
  <pre><code>class RunningAverager
  {
      public:
          RunningAverager(uint64_t FilterOrder)
          {
              for (uint64_t i=0; i&lt;FilterOrder; i++)
              {
                  Registers.push(0);
              }
          }
          uint64_t GetAverage(uint64_t NewInput)
          {
              Registers.push(NewInput);
              Registers.pop();
              return GetAverage();
          }
          uint64_t GetAverage() const
          {
              uint64_t Sum = 0;
              //for (uint64_t i=0; i&lt;Registers.size(); i++)       &lt;-- Works
              for (std::stack&lt;uint64_t&gt;::const_reference ref=Registers.begin(); ref!=Registers.end(); ???)
              {   // begin() and end() methods do not exist for std::stack
                  //Sum += Registers[i];      Doesn't work, because the [] operator is not overloaded.
                  Sum += ref;
              }
              return Sum / Registers.size();
          }
      private:
          std::stack&lt;uint64_t&gt; Registers;
  };
  </code></pre>
  
  <p>I am having trouble iterating the <code>std::stack</code> object <code>Registers</code>. Unlike other STL containers, it doesn't give an iterator or random access operator.</p>
  
  <p>How do I loop and <code>std::stack</code> object?</p>
  
  <p>An example usage:</p>
  
  <pre><code>RunningAverager ra(10);
  
  while(...)
  {
      FilteredSpeed = ra.GetAverage(ActualSpeed);
  }
  </code></pre>