STL- 常用算法

tech2026-08-06  3

常用遍历算法

学习目标:

掌握常用的遍历算法

算法简介:

for_each //遍历容器transform //搬运容器到另一个容器中 #include <iostream> #include <algorithm> #include <vector> using namespace std; void print01(int val) { cout << val << " "; } //函数对象 class print02 { public: void operator()(int val) const { cout << val << " "; } }; //for_each算法基本用法 void test01() { vector<int> 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(); return 0; }
最新回复(0)