Results 1 to 3 of 3

Thread: count_if and for_each

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default count_if and for_each

    Hello,

    I need to do something like this (that doens't compile); but anyway It's a bit wrong:
    Qt Code:
    1. class Gene {
    2. char _allele;
    3. char getAllele() { return _allele; }
    4. ...................................
    5. class Chromosome {
    6. static const int _length = 5;
    7. vector<Gene> _reppresentation;
    8. vector<Gene>& getReppresentation() { return _reppresentation; }
    9. ..........................................
    10.  
    11. class Population {
    12. public:
    13. vector<Chromosome> _chsome;
    14. void computeFitnesses() {
    15. //ni is wrong; I need something like a vector<int> to store every;
    16. int ni = std::for_each( _chsome.begin(), _chsome.end(), Fitness() ).getCount();
    17. }
    18.  
    19. class Fitness {
    20. Gene _g;
    21. int _count;
    22. public:
    23. Fitness() { }
    24. void operator()(Chromosome g) {
    25. _count = std::count_if( g.getReppresentation().begin(), g.getReppresentation().end(), fitness); //it doens't compile here on 'fitness'
    26.  
    27. }
    28. bool fitness() { return _g.getAllele() == '1'; }
    29. int getCount() { return _count; }
    30. };
    To copy to clipboard, switch view to plain text mode 
    For each chromosome I need to return the number of '1'. Any helps, please?
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: count_if and for_each

    count_if takes a predicate as third argument, i.e a function that takes one argument (object to be checked) and the function you supply does not take any argument. Also if the fitness is a fixed value (1 in your example) you can directly use that value instead of a predicate function.

    could you explain what's wrong with foreach ?

    last, but not least there is a small syntax error (not in the C++ code but in the english names : it's representation not reppresentation).
    Current Qt projects : QCodeEdit, RotiDeCode

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: count_if and for_each

    fitness() should probably be either a function or a static method. Or you can run a simple for loop instead of that silly count_if. It completely doesn't make sense to use it here.

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.