文章目录
前言
-
- 前言
- 示例demo
由于偶然间发现for_each能使得避免使用for循环,大大简化了代码。这里简单记录下for_each的一个简单示例demo,方便温习。
示例demo#include #include "vector" #include "algorithm" void myfunc1(int i) { std::cout << i << " "; } int main() { std::vector<int> arr{ 3, 4, 2, 6, 5, 1 }; std::cout << "based on for_each, print the vector\n"; for_each(arr.begin(), arr.end(), myfunc1); std::cout << "\n"; std::cout << "based on lambda, print the vector\n"; for_each(arr.begin(), arr.end(), [](const auto &i){std::cout << i << " ";}); return 0; }