std::for

std::for,第1张

std::for

描述:
       遍历容器元素并执行指定函数

定义:

template< class InputIt, class UnaryFunction >
UnaryFunction for_each( InputIt first, InputIt last, UnaryFunction f );

template< class InputIt, class UnaryFunction >
constexpr UnaryFunction for_each( InputIt first, InputIt last, UnaryFunction f );

可能的实现:

template
UnaryFunction for_each(InputIt first, InputIt last, UnaryFunction f)
{
    for (; first != last; ++first) {
        f(*first);
    }
    return f; // C++11 起隐式移动
}

参数:
       first, last - 容器的迭代器范围
       f - 函数对象,要应用于解引用范围 [first, last) 中每个迭代器结果的函数

示例:

#include 
#include 
#include 

int main()
{
	std::vector vecData = { 1,2,3 };
	std::for_each(std::begin(vecData),std::end(vecData),
		[](int nData)
	{
		std::cout << "元素为:" << nData << std::endl;
	});
	
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5698430.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存