#include <vector>
using namespace std;
class A
{
public:
A() = default;
void Add()
{
a++;
}
private:
int a;
};
int main()
{
vector<A> x(10);
for (auto pos = x.begin(); pos != x.end(); ++pos) pos->Add();
}
for_each
似乎是非修改性的。http://en.cppreference.com/w/cpp/algorithm/for_each
f - 函数对象,应用于范围[first,last)中每个迭代器解引用的结果
函数的签名应等同于以下内容:
void fun(const Type &a);
签名不需要具有const&。类型Type必须是这样的,即InputIt类型的对象可以被取消引用,然后隐式转换为Type。
所以,我的问题是:
是否有标准函数/方法可以执行与for(auto pos = x.begin(); pos!= x.end(); ++pos)pos-&gt; Add();
相同的操作?