How do you use stl functions like for_each?

I started using stl containers because they were very useful when I needed list, set and map functionality, and I had nothing else in my programming environment. I did not care about the idea behind it. The STL documentation was interesting until when it came to functions, etc. Then I skipped reading and just used containers.

But yesterday, still relaxing from my holidays, I just tried and wanted to go a little more. So I used the conversion function (can I get applause for me, thanks).

From an academic point of view, it looked interesting, and it worked. But what bothers me is that if you activate the use of these functions, you need thousands of helper classes, mainly for everything you want to do in your code. The whole logic of the program is cut into tiny pieces. This slicing is not the result of good coding habits; it is just a technical necessity. Something that makes my life is probably harder.

I have studied diligently that you should always choose the simplest approach that solves the problem. I don’t see what, for example, the for_each function does for me, which justifies using a helper class over a few simple lines of code that are inside a regular loop so that everyone can see what happens.

I would like to know what you think about my problems? You saw this, as I did when you started working in this way and changed your mind when you got used to it? Are there any benefits that I forgot? Or you just ignore this stuff, just like me (and probably continue to do so).

Thank.

PS: I know that there is a real for_each loop in boost. But I ignore it here, since it is just a convenient way for my regular iterator loops, I think.

+5
source share
8 answers

The whole logic of the program is cut into tiny pieces. This slicing is not the result of good coding habits. It is just a technical necessity. Something that makes my life is probably harder.

, . ++ -, - :

std::for_each(vec.begin(), vec.end(), [&](int& val){val++;})

, , . , , , . , , , , , .

:

int incr(int& val) { return val+1}

// and at the call-site
std::for_each(vec.begin(), vec.end(), incr);

, :

  • ( )

, , . , . , incr . , .

+7

boost::bind boost::lambda, . :

class A
{
public:
    A() : m_n(0)
    {
    }

    void set(int n)
    {
        m_n = n;
    }

private:
    int m_n;
};

int main(){    

    using namespace boost::lambda;

    std::vector<A> a;
    a.push_back(A());
    a.push_back(A());

    std::for_each(a.begin(), a.end(), bind(&A::set, _1, 5));


    return 0;
}
+3

, , for_each transform . STL .

Boost -, , , , . , , .

:

for (Range::const_iterator i = r.begin(), end = r.end(); i != end(); ++i)
{
   *out++ = ..   // for transform
}

for_each transform, : sort, unique, rotate, .

+3

for_each.

, , .

:

// assume some SinkFactory class is defined
// and mapItr is an iterator of a std::map<int,std::vector<SinkFactory*> >

std::for_each(mapItr->second.begin(), mapItr->second.end(),
    checked_delete<SinkFactory>);

checked_delete boost, :

template<typename T>
void checked_delete(T* pointer)
{
    delete pointer;
}

:

for(vector<SinkFactory>::iterator pSinkFactory = mapItr->second.begin();
    pSinkFactory != mapItr->second.end(); ++pSinkFactory)
    delete (*pSinkFactory);

, checked_delete, ( boost), aywhere, , , ( , vector<SinkFactory>::iterator pSinkFactory).

- , for_each container.end() , , for_each ( - ).

, boost:: bind stl-, (. http://www.boost.org/doc/libs/1_43_0/libs/bind/bind.html#with_algorithms).

+2

, ++ comity . ++ 0x lambdas. .

std::transform(in.begin(), int.end(), out.begin(), [](int a) { return ++a; })
+1

- . :

void IncreaseVector(std::vector<int>& v)
{
 class Increment
 {
 public:
  int operator()(int& i)
  {
   return ++i;
  }
 };

 std::for_each(v.begin(), v.end(), Increment());
}

IMO, , . , , . , , . , .

+1

, ++ ( "++ 0x" ), , 2011 . ++ , ++ lambdas, , , , . Lambdas (?) GCC GCC 4.5.

0

, STL Boost, , .

As a user of these libraries - you do not plan to redo .NET, do you? - You can use their simplified goodies.

Here is a possibly simpler foreach from Boost that I like to use:

BOOST_FOREACH(string& item in my_list)
{
    ...
}

It looks much neater and simpler than when used .begin(), .end()etc., but still it works for almost any iterative collection (and not just arrays / vectors).

0
source

All Articles