H'H
H'H

Reputation: 1678

C++98 Valid use of "for_each" in the code

I want to go directly in the code; Here is my struct:

struct pnt {
  mesh::Point _point;
  pnt_type _type;
  bool _aux;
  };

enum NNSerach {A_NN = 0, B_NN, C_NN, ALL}; 

and here is my function:

void typeDetection( pnt& PNT, const NNSerach NNType, const fieldclass& field )

Here is my for loop inside a member function of the fieldclass.

vector< pnt > oldpnTs;
...
  for(size_t iter = 0; iter < oldpnTs.size(); iter++ )
  {
    typeDetection(oldpnTs[iter], ALL, *this);
  }

Is it possible to use for_each here, when my vector member is only one of the arguments of the applied function?

EDIT: I can only use C++98 I want to apply typeDetection function for each member of the oldpnTs vector.

Upvotes: 1

Views: 2830

Answers (3)

davidhigh
davidhigh

Reputation: 15468

Yes, that should work (though I couldn't test it because nothing is really defined here). Instead of this part of your code,

   vector< pnt > oldpnTs;

   for(size_t iter = 0; iter < oldpnTs.size(); iter++ )
   {
     typeDetection(oldpnTs[iter], ALL, *this);
   }

you can try this (in C++14):

   vector< pnt > oldpnTs;
   NNSerach all = ALL;
   // ...
   std::for_each(oldpnTs.begin(), oldpnTs.end()
               , [this, all](auto& x) {this->typeDetection(x, all, *this);});



EDIT: in C++98, instead of the lambda you surely can use a functor where you fix the other variables beside pnt&. Or you can use boost::lambda or boost::bind.

Upvotes: 1

undermind
undermind

Reputation: 1841

As you use C++98, you should implement lambda function yourself using a functor:

#include <vector>
#include <algorithm>

struct pnt {
  mesh::Point _point;
  pnt_type _type;
  bool _aux;
    };

enum NNSerach {A_NN = 0, B_NN, C_NN, ALL}; 

class fieldclass;

class TypeDetection
{
public:
    TypeDetection(const NNSerach NNType, const fieldclass& field) : _NNType(NNType), _field(field){}
    void operator() (pnt& PNT)
    {
        typeDetection(PNT, _NNType, _field);
    }
private:
    void typeDetection( pnt& PNT, const NNSerach NNType, const fieldclass& field ){}

    const fieldclass& _field;
    const NNSerach _NNType;
};

class fieldclass
{
public:
    void Do()
    {
        std::vector<pnt> oldpnTs;
        std::for_each(oldpnTs.begin(), oldpnTs.end(), TypeDetection(ALL, *this));
    }
};

Upvotes: 0

JBL
JBL

Reputation: 12907

You could, though that wouldn't make it that much better I'd say, as you'd need to define a functor for typeDetection, and then pass it to for_each (don't forget to pass this and the NNSearch value to the functor when you create it so you can pass them to typeDetection when needed).

In this case I'd consider keeping the for loop as it is, as the code is readable in its current form, unless you have to perform this operation on multiple such vectors with potentially different sizes, resulting in the following:

std::for_each(std::begin(oldpnTs), std::end(oldpnTs), typeDetectionFunctor);
std::for_each(std::begin(oldpnTs), std::end(oldpnTs2), typeDetectionFunctor);
std::for_each(std::begin(oldpnTs), std::end(oldpnTs3), typeDetectionFunctor);
std::for_each(std::begin(oldpnTs), std::end(oldpnTs4), typeDetectionFunctor);
//...

instead of multiple 4 liners loop like you currently have.

Upvotes: 2

Related Questions