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

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -