建设银行代发工资清单网站,企业网站建设费用需要多少钱,个人网页设计欣赏作品,东软集团建设网站算法概述: 算法主要是由头文件 algorithm functional numeric 组成。 algorithm 是所有STL头文件中最大的一个#xff0c;范围涉及到比较、 交换、查找、遍历操作、复制、修改等等 numeric 体积很小#xff0c;只包括几个在序列上面… 算法概述: 算法主要是由头文件 algorithm functional numeric 组成。 algorithm 是所有STL头文件中最大的一个范围涉及到比较、 交换、查找、遍历操作、复制、修改等等 numeric 体积很小只包括几个在序列上面进行简单数学运算的模板函数 functional 定义了一些模板类,用以声明函数对象。 for_each
是 C 标准模板库STL中的一个算法用于对一个范围内的每个元素应用一个函数。以下是简要解释和一个示例 std::for_each 语法 template class InputIterator, class Function
Function for_each(InputIterator first, InputIterator last, Function fn);参数: first、last: 表示范围的输入迭代器。fn: 接受范围内元素的一元函数。 返回值: fn函数对象。 代码示例1:
#include iostream
#include vector
#include algorithmvoid printSquare(int x) {std::cout x * x ;
}int main() {std::vectorint numbers {1, 2, 3, 4, 5};// 使用 std::for_each 打印每个元素的平方std::for_each(numbers.begin(), numbers.end(), printSquare);return 0;
}代码示例2:
#include iostream
#include algorithm
#include vectorusing namespace std;
//普通函数
void print01(int val)
{cout val ;
}
//函数对象
class print02
{
public:void operator()(int val){cout val ;}
};
//for_each算法基本用法
void test01() {vectorint v;for (int i 0; i 10; i){v.push_back(i);}//遍历算法for_each(v.begin(), v.end(), print01);cout endl;for_each(v.begin(), v.end(), print02());cout endl;
}
int main() {test01();system(pause);return 0;
}