36 lines
678 B
Markdown
36 lines
678 B
Markdown
---
|
||
tags: cpp17
|
||
aliases:
|
||
date: 2025-02-10
|
||
time: 17:37:31
|
||
description:
|
||
---
|
||
|
||
for_each 是一個 function,它的原型是:
|
||
```cpp
|
||
template<class InputIterator, class Function>
|
||
Function for_each(
|
||
InputIterator _Start,
|
||
InputIterator _Last,
|
||
Function _Func
|
||
);
|
||
```
|
||
|
||
它需要3個參數,第1個是開始的iterator,第2是結束的 iterator,第3個是要用來處理的 function。
|
||
|
||
一個簡單的例子,有一個array,需要把每一個數都加1:
|
||
```cpp
|
||
vector<int> arr1 = { 4, 5, 8, 3, 1 };
|
||
|
||
for_each(
|
||
arr1.begin(), // _Start
|
||
arr1.end(), // _Last
|
||
[](int& val) { // _Func
|
||
val += 1;
|
||
}
|
||
);
|
||
```
|
||
|
||
|
||
# 參考來源
|