Results 1 to 7 of 7

Thread: accumulate variant

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: accumulate variant

    The code is basically correct (assuming that's just part of it).
    (If it is supposed to be complete: Other is all private, the constructors are missing etc.)


    When I add that missing stuff (hint: do not forget to initialize _missed) the variable mean is assigned a meaningful value (namely the sum of my Values).

    What is the problem you're having?

  2. #2
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: accumulate variant

    I need sum all "valid" values (exactly what it do) BUT I need to compute the "MEAN"; so I need to know how many "values" I've summed; how know how many they?
    Regards

  3. #3
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: accumulate variant

    ok... you can do that by either:
    * counting those not _missed (and thus iterating twice over your vector)
    * or, by using a function object that counts both values and missed
    (* or you could use globals, which is very bad imho)

    try to replace your function by

    Qt Code:
    1. class MeanAccumulator
    2. {
    3. int counted_;
    4. double sum_;
    5. public:
    6. MeanAccumulator() : counted_(0), sum_(0) {}
    7. void operator() (const Value &right)
    8. {
    9. if ( right.getMissed() ) return;
    10. ++counted_;
    11. sum_ += right._value;
    12. }
    13. double mean() const {return counted_ ? sum_/counted_ : 0; }
    14. };
    15.  
    16. void computeMean() {
    17. MeanAccumulator res = std::for_each( _values.begin(), _values.end(),
    18. MeanAccumulator());
    19. std::cerr << "sum="<<res.mean()<<std::endl;
    20. }
    To copy to clipboard, switch view to plain text mode 

    Note that the function object passed into for_each does NOT get modified.
    The resulting function object (with the updated state) is returned by for_each.

    (I assumed you do not insist on using accumulate.)

    HTH

Similar Threads

  1. Replies: 8
    Last Post: 16th July 2008, 14:05
  2. Automating Excel 97
    By Aki-Matti in forum Qt Programming
    Replies: 0
    Last Post: 14th December 2007, 06:28
  3. pixmap from variant
    By kernel_panic in forum Qt Programming
    Replies: 1
    Last Post: 30th January 2007, 10:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.