accumulate: variant

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mickey0
    New Member
    • Jan 2008
    • 142

    accumulate: variant

    hello,
    I need to sum a vector and divide the sum by the number of element:
    if the filed _missed is "true", the value mustn't take into account....
    Code:
    class Value {	
    public:
    	double _value;
    	bool _missed;
            bool getMissed() const { return _missed; }
            //other methods....
    };
    class Other {
        vector<Value> _values;
        static double sum_values(const double& accum, const Value& right) {
    	 if ( right.getMissed() ) return accum;
    	 return accum + right._value;
        }
        void computeMean() {
              double mean = std::accumulate( _values.begin(), _values.end(), 0.0,   
              Other::sum_values );
        }
    };
    My accumulate compute the sum properly but HOW can I retrive the numbers summed???

    Thanks,
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Maybe as you sum the Values you could push_back the Value into a vector that you could return.

    BTW: Your sum_values is a re-code of the std::accumulate s algorithm.

    Comment

    • mickey0
      New Member
      • Jan 2008
      • 142

      #3
      yes but there is this line too: "if ( right.getMissed () )"
      I don't know how code your idea, because I don't usually use accumulate and alghorithms; could you write me an example, please?

      thanks,

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Read this: http://msdn.microsoft.com/en-us/library/3bsk3zs9.aspx.

        Comment

        Working...