Thursday 17 December 2015

Convert for_each to for

In c++11, you can use for_each as std::for_each(fts.begin(), fts.end(), [&](Feature* i){do something});, which is really convenient. However, this requires using -std=c++11 in cmake and it somehow conflicts with SSBA.

In order to convert for_each to for loop and avoiding to use -std=c++11, there are several things to do. Take the setExistingFeatures defined here for example,  we have to change const Features& fts to Features& fts first, otherwise an error of "conversion from const requested" will appear.

Next, for fts.begin(), fts.end(), we have to define an iterator, std::list<Feature*>::iterator it = fts.begin(); it != fts.end(); it++, which could be used in the for loop.

Finally note that it is a pointer to a structure, to access the members in the structure, we have to use (*it)->px[1].

No comments:

Post a Comment