C++ 如何将 std::foreach 与参数/修改一起使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/476488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use std::foreach with parameters/modification
提问by Jesse Beder
I've found myself writing
我发现自己在写作
for(int i=0;i<myvec.size();i++)
myvec[i]->DoWhatever(param);
a lot, and I'd like to compress this into a foreach
statement, but I'm not sure how to get param
in there without going super-verbose. I've also got things like
很多,我想把它压缩成一个foreach
声明,但我不知道如何param
在不冗长的情况下进入那里。我也有类似的东西
for(int i=0;i<myvec.size();i++)
if(myvec[i]->IsOK())
myvec[i]->DoWhatever(param);
and I'd like to rewrite that guy too. Any thoughts?
我也想重写那个人。有什么想法吗?
Oh, also, for various reasons, I don't want to use boost.
哦,还有,出于各种原因,我不想使用boost。
采纳答案by Martin York
#include <vector>
#include <algorithm>
#include <functional>
class X
{
public:
void doWhat(int x) {}
bool IsOK() const {return true;}
};
class CallWhatIfOk
{
public:
CallWhatIfOk(int p): param(p) {}
void operator()(X& x) const
{ if (x.IsOK()) {x.doWhat(param);}}
private:
int param;
};
int main()
{
std::vector<X> myVec;
std::for_each( myVec.begin(),
myVec.end(),
std::bind2nd(std::mem_fun_ref(&X::doWhat),4)
);
std::for_each( myVec.begin(),
myVec.end(),
CallWhatIfOk(4)
);
}
回答by Konrad Rudolph
Oh, also, for various reasons, I don't want to use boost.
哦,还有,出于各种原因,我不想使用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.
正确的决定,但很可能是错误的决定。将 Boost 视为 STL 的扩展。C++ 是一种库驱动的语言。如果你不考虑这一点,你的代码质量就会低下。
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 mucheasier:
虽然std::for_each
可以在这里使用,但在 C++ 0x 之前,C++ 中缺少 lambda 表达式使这变得乏味。我提倡使用Boost.ForEach!它使这更容易:
foreach (yourtype x, yourvec)
if (x.IsOK())
x.Whatever();
回答by jalf
My preferred solution is usually to write a functor to do what I need:
我的首选解决方案通常是编写一个函子来做我需要的事情:
struct doWhatever {
doWhatever(const Param& p) p(p) {}
void operator(MyVec v&, Param p) {
v.DoWhatever(param);
}
private:
Param p;
};
And then the loop:
然后循环:
std::for_each(myvec.begin(), myvec.end(), doWhatever(param));
Depending on how many variations of this you have, this might be a bit too verbose. There are plenty of options for doing it inline though. boost::lambda would let you construct the function you need at the call-site. boost::bind (or the standard library bind functions) would let you bind the parameter param to the function so you don't need to supply it as an argument every time.
根据您有多少变体,这可能有点过于冗长。不过,有很多选项可以进行内联操作。boost::lambda 可以让你在调用站点构建你需要的函数。boost::bind(或标准库绑定函数)可以让你将参数 param 绑定到函数,这样你就不需要每次都将它作为参数提供。
boost::lambda is probably the most concise and flexible approach. I usually use the plain functor approach because the syntax is easier to remember. ;)
boost::lambda 可能是最简洁、最灵活的方法。我通常使用普通的函子方法,因为语法更容易记住。;)
回答by Rick
well when we have compilers that support C++0x lambda expresions, this becomes straightforward and minimally invasive:
好吧,当我们拥有支持 C++0x lambda 表达式的编译器时,这将变得简单且侵入性最小:
std::for_each(myvec.begin(),myvec.end(),[&](X& item){
item->DoWhatever(param);
});
and the second example may look like this:
第二个示例可能如下所示:
std::for_each(myvec.begin(),myvec.end(),[&](X& item){
if(item->IsOK())
myvec[i]->DoWhatever(param);
});
回答by psaghelyi
#include <vector>
#include <algorithm>
#include <boost/bind.hpp>
#include <boost/lambda/if.hpp>
#include <boost/lambda/bind.hpp>
struct A
{
bool IsOK () { return true; }
void DoWhatever (int param) {}
};
struct B
{
bool IsOk (A * a) { return true; }
void DoWhatever (A * a, int param) {}
};
typedef std::vector<A *> Myvec;
void main()
{
Myvec myvec;
int param = 1;
B b;
// first challenge using boost::bind (fnct in the same class)
std::for_each (myvec.begin(), myvec.end(),
boost::bind (&A::DoWhatever, _1, param));
// first challenge using boost::bind (fnct in an external class)
std::for_each (myvec.begin(), myvec.end(),
boost::bind (&B::DoWhatever, &b, _1, param));
// second challange using boost::lambda (fnct in the same class)
std::for_each (myvec.begin(), myvec.end(),
boost::lambda::if_then(
boost::lambda::bind (&A::IsOK, boost::lambda::_1),
boost::lambda::bind (&A::DoWhatever, boost::lambda::_1, param)
)
);
// second challange using boost::lambda (fnct in an external class)
std::for_each (myvec.begin(), myvec.end(),
boost::lambda::if_then(
boost::lambda::bind (&B::IsOK, &b, boost::lambda::_1),
boost::lambda::bind (&B::DoWhatever, &b, boost::lambda::_1, param)
)
);
}
You can simplify it by using namespaces...
您可以通过使用名称空间来简化它...
回答by Mircea Ispas
If you are using GCC you can define something like:
如果您使用 GCC,您可以定义如下内容:
#define foreach(element, array) \
for(typeof((array).begin()) element = (array).begin(), __end_##element = (array).end();\
element != __end_##element;\
++element)
and use it after like this:
然后像这样使用它:
foreach(element, array){
element->DoSomething(); //or (*element)->DoSomething() if type is already a pointer
}
I use this on a custom array but it works fine with std::vector too.
我在自定义数组上使用它,但它也适用于 std::vector。