C++ STL中for_each算法和迭代器循环的区别?

void scored(vector<Person>& g)
{    
    //foreach不能修改
    for_each(g.begin(), g.end(), [](Person p){p.m_score = 100;});
    //迭代器可以修改
    for(vector<Person>::iterator it=g.begin(); it != g.end(); it++)
    {
        (*it).m_score = 100;
    }
}

我看stl源码也只是做了个*操作

// for_each

 

template <class _InputIterator, class _Function>

inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17

_Function

for_each(_InputIterator __first, _InputIterator __last, _Function __f)

{

for (; __first != __last; ++__first)

__f(*__first);

return __f;

}

 

#if _LIBCPP_STD_VER > 14

 

感谢!

在lambda 函数里面传递引用试试