You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
cout << "There are not two adjacent elements where the "
125
-
<< "second is twice the first." << endl;
125
+
<< "second is twice the first." << endl;
126
126
else
127
+
{
127
128
cout << "There are two adjacent elements where "
128
-
<< "the second is twice the first."
129
-
<< "\n They have values of " << *(result2++);
130
-
cout << " & " << *result2 << "." << endl;
129
+
<< "the second is twice the first.\n"
130
+
<< "They have values of " << *(result2++)
131
+
<< " & " << *result2 << "." << endl;
132
+
}
131
133
}
132
134
```
133
135
@@ -164,12 +166,47 @@ A condition to test for. This is a user-defined predicate function object that d
164
166
165
167
### Return Value
166
168
167
-
Returns **true** if the condition is detected at each element in the indicated range, and **false** if the condition is not detected at least one time.
169
+
Returns **true** if the condition is detected at each element in the indicated range or if the range is empty, and **false** otherwise.
168
170
169
171
### Remarks
170
172
171
173
The template function returns **true** only if, for each `N` in the range `[0,Last - first)`, the predicate `comp(*(_First + N))` is **true**.
172
174
175
+
### Example
176
+
177
+
```cpp
178
+
// alg_all_of.cpp
179
+
// compile with: /EHsc
180
+
#include <list>
181
+
#include <algorithm>
182
+
#include <iostream>
183
+
184
+
int main()
185
+
{
186
+
using namespace std;
187
+
188
+
list<int> li { 50, 40, 10, 20, 20 };
189
+
list<int>::iterator iter;
190
+
191
+
cout << "li = ( ";
192
+
for (iter = li.begin(); iter != li.end(); iter++)
193
+
cout << *iter << " ";
194
+
cout << ")" << endl;
195
+
196
+
// Check if all elements in li are even.
197
+
auto is_even = [](int elem){ return !(elem % 2); };
198
+
if (all_of(li.begin(), li.end(), is_even))
199
+
cout << "All the elements are even numbers.\n";
200
+
else
201
+
cout << "Not all the elements are even numbers.\n";
202
+
}
203
+
```
204
+
205
+
```Output
206
+
L = ( 50 40 10 20 20 )
207
+
All the elements are even numbers.
208
+
```
209
+
173
210
## <a name="any_of"></a> any_of
174
211
175
212
Returns **true** when a condition is present at least once in the specified range of elements.
@@ -203,6 +240,40 @@ The template function returns **true** only if, for some `N` in the range
203
240
204
241
`[0, last - first)`, the predicate `comp(*(first + N))` is true.
Tests whether there is an element in a sorted range that is equal to a specified value or that is equivalent to it in a sense specified by a binary predicate.
0 commit comments