c++ - Iterating over list of structs -


i'm creating list of structs:

struct task{     int task_id;     bool is_done;     char* buffer;     int length;  } task; list<task> tasklist; 

and trying iterate on tasks in order check is_done status:

    (std::list<task>::const_iterator iterator = tasklist.begin(), end = tasklist.end(); iterator != end; ++iterator) {          if(iterator->is_done) {             return 1;         } else {             return 2;         }     } 

where wrong? get: missing template argument before '->' token

the iterator's operator-> dereferencing already. instead of

if(*iterator->is_done==true) 

you need

if(iterator->is_done==true) 

is equivalent to

if((*iterator).is_done==true) 

which sidenote equivalent easier read

if((*iterator).is_done) 

or

if(iterator->is_done) 

. better, use std::any_of:

#include <algorithm>  ....  if (any_of(begin(tasklist), end(tasklist),      [](task const &t) { return t.is_done; })) {     return 1; } else {     return 2; } 

informal note: there no need qualify any_of, begin , end std::, because tasklist of type std::list<?>, , c++-compiler functions in std-namespace already.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -