title: Summing struct members inside a vector
body: <p>Consider the following:</p>
  
  <pre><code>typedef struct {
      int a;
      int b;
      int c;
      int d;
  } ABCD;
  
  typedef std::vector&lt;ABCD&gt; VecABCD;
  </code></pre>
  
  <p>Say I wanted to add up every 'a' member in a vector of type VecABCD.  Easy!  I just loop through the vector, and sum as I go.</p>
  
  <pre><code>int CalcSumOfA(const VecABCD &amp;vec)
  {
      int sumOfA = 0;
      VecABCD::const_iterator it;
      for(it=vec.begin();it!=vec.end();it++)
          sumOfA += it-&gt;a;
      return sumOfA;
  }
  </code></pre>
  
  <p>Say I wanted to do the same thing with 'b'?  Easy!  I'd write....essentially the same function, but with only trivial changes.  Same with 'c' and 'd'.</p>
  
  <p>So, is there a more concise, less repetitive way of doing this?  I'd like to do something like:</p>
  
  <pre><code>int sumOfA = SumOfMembers(myVec, a);
  </code></pre>
  
  <p>but I can't conceive of how I'd put such a function together.  Ideally, it'd be a template, and I could use it with a vector of any kind of structure, not specifically bound to VecABCD.  Anyone have any ideas?</p>