How to use std::foreach with parameters/modification

前端 未结 6 2285
难免孤独
难免孤独 2020-12-23 11:34

I\'ve found myself writing

for(int i=0;iDoWhatever(param);

a lot, and I\'d like to compress this into

6条回答
  •  太阳男子
    2020-12-23 12:19

    Oh, also, for various reasons, I don't want to use boost.

    Valid decision, but most likely the wrong one. Consider Boost as an extension to the STL. C++ is a library-driven language. If you don't take this into account, your code will be qualitatively inferior.

    While std::for_each can be used here, the absence of lambda expressions in C++ until C++0x makes this tedious. I advocate using Boost.ForEach! It makes this much easier:

    foreach (yourtype x, yourvec)
        if (x.IsOK())
            x.Whatever();
    

提交回复
热议问题