SumSecondFunction.h
Go to the documentation of this file.
1 /// SumSecondFunction
2 /// 28-Jul-2009 William Seligman <seligman@nevis.columbia.edu>
3 
4 /// When using STL, maps, and the std::accumulate function, there's one
5 /// function I keep coding over and over again: accumulate the second
6 /// member of a pair.
7 ///
8 /// To save myself time (and others who know and use STL), here's a
9 /// complete STL-compatible implementation of that function. To use
10 /// it, assume you have an object:
11 ///
12 /// std::map<K,V> myMap;
13 ///
14 /// To sum all the V's in the map:
15 ///
16 /// V sum = std::accumulate( myMap.begin(),myMap.end(),V(),SumSecondFunction<K,V>() );
17 ///
18 /// (Yes, I know there are other, better ways to do this, if one has
19 /// access to BOOST. Unfortunately, we're not supposed to use BOOST in
20 /// LArSoft, since as of Jul-2009 it's not universally installed on
21 /// FNAL machines.)
22 ///
23 #ifndef Utilities_SumSecondFunction_h
24 #define Utilities_SumSecondFunction_h
25 
26 #include <functional>
27 
28 namespace util {
29 
30  template < typename _Key, typename _Value, typename _BinaryOperation = std::plus<_Value> >
32  : public std::binary_function< _Value,
33  std::pair<_Key, _Value>,
34  _Value >
35  {
36  public:
37  const _Value operator() ( const _Value& value,
38  const std::pair<_Key, _Value>& entry ) const
39  {
40  return _BinaryOperation()( value, entry.second );
41  }
42  };
43 
44 } // namespace util
45 
46 #endif // Utilities_SumSecondFunction_h
47 
48 
Namespace for general, non-LArSoft-specific utilities.
QList< Entry > entry
const _Value operator()(const _Value &value, const std::pair< _Key, _Value > &entry) const