-
Notifications
You must be signed in to change notification settings - Fork 967
for_each_n
doc completed (#3933)
#3934
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
84b6921
dc6c216
c49abaa
b1dda12
dc9b070
f39cf26
1c7cd07
17755d4
a8cb5da
b70cf0c
b07e312
e9bd18f
89a1ca0
fd0c089
4c03f9e
ff95925
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -2191,7 +2191,7 @@ int main() | |||||||
|
||||||||
// The function object is templatized and so can be | ||||||||
// used again on the elements with a different Factor | ||||||||
for_each (v1.begin( ), v1.end( ), MultValue<int> (5 ) ); | ||||||||
for_each ( v1.begin( ), v1.end( ), MultValue<int> ( 5 ) ); | ||||||||
|
||||||||
cout << "Multiplying the elements of the vector v1mod\n " | ||||||||
<< "by the factor 5 gives:\n v1mod2 = ( " ; | ||||||||
|
@@ -2223,19 +2223,123 @@ Average ( v1mod2 ) = 10. | |||||||
|
||||||||
## <a name="for_each_n"></a> `for_each_n` | ||||||||
|
||||||||
Applies a specified function object to a specified number of elements in a range beginning with a particular element. | ||||||||
|
||||||||
```cpp | ||||||||
template<class InputIterator, class Size, class Function> | ||||||||
InputIterator for_each_n( | ||||||||
InputIterator first, | ||||||||
Size n, | ||||||||
Function f); | ||||||||
Size count, | ||||||||
Function func); | ||||||||
|
||||||||
template<class ExecutionPolicy, class ForwardIterator, class Size, class Function> | ||||||||
ForwardIterator for_each_n( | ||||||||
ExecutionPolicy&& exec, | ||||||||
ForwardIterator first, | ||||||||
Size n, | ||||||||
Function f); | ||||||||
Size count, | ||||||||
Function func); | ||||||||
``` | ||||||||
|
||||||||
### Parameters | ||||||||
|
||||||||
*`exec`*\ | ||||||||
The execution policy to use. | ||||||||
|
||||||||
*`first`*\ | ||||||||
An input iterator addressing the position of the first element in the range to be operated on. | ||||||||
|
||||||||
*`count`*\ | ||||||||
A signed or unsigned integral type specifying the number of elements to be operated on. | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The detail is correct but it may be more consumable to simply say 'The number of elements to operate on' |
||||||||
|
||||||||
*`func`*\ | ||||||||
User-defined function object that is applied to each element in the range [`first`, `first` + `count`). | ||||||||
|
||||||||
### Return value | ||||||||
|
||||||||
An iterator to the element that follows the last element processed if *`count`* > zero, | ||||||||
otherwise the first element. | ||||||||
Comment on lines
+2259
to
+2260
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is unnecessarily confusing, imo; I would say something more like:
Suggested change
|
||||||||
|
||||||||
### Remarks | ||||||||
|
||||||||
The range referenced must be valid; all pointers must be dereferenceable and, within the sequence, the last position must be reachable from the first by incrementation. | ||||||||
BrandonPacewic marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
The complexity is linear with at most *`count`* operations. | ||||||||
BrandonPacewic marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
### Example | ||||||||
|
||||||||
BrandonPacewic marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
```cpp | ||||||||
// alg_for_each_n.cpp | ||||||||
// compile with: /EHsc | ||||||||
#include <vector> | ||||||||
#include <algorithm> | ||||||||
#include <iostream> | ||||||||
|
||||||||
// The function object multiplies an element by a Factor | ||||||||
template <class Type> | ||||||||
class MultValue | ||||||||
{ | ||||||||
private: | ||||||||
Type Factor; // The value to multiply by | ||||||||
public: | ||||||||
// Constructor initializes the value to multiply by | ||||||||
MultValue ( const Type& value ) : Factor ( value ) { | ||||||||
} | ||||||||
|
||||||||
// The function call for the element to be multiplied | ||||||||
void operator( ) ( Type& elem ) const | ||||||||
{ | ||||||||
elem *= Factor; | ||||||||
} | ||||||||
}; | ||||||||
|
||||||||
int main() | ||||||||
{ | ||||||||
using namespace std; | ||||||||
vector<int> v; | ||||||||
vector<int>::iterator Iter; | ||||||||
|
||||||||
// Constructing vector v | ||||||||
int i; | ||||||||
for ( i = -4 ; i <= 2 ; i++ ) | ||||||||
BrandonPacewic marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
{ | ||||||||
v.push_back( i ); | ||||||||
} | ||||||||
|
||||||||
cout << "Original vector v = ( " ; | ||||||||
for ( Iter = v.begin( ) ; Iter != v.end( ) ; Iter++ ) | ||||||||
cout << *Iter << " "; | ||||||||
cout << ")." << endl; | ||||||||
|
||||||||
// Using for_each_n to multiply the first 3 elements by a Factor, | ||||||||
// saving position | ||||||||
auto pos = for_each_n ( v.begin( ), 3, MultValue<int> ( -2 ) ); | ||||||||
|
||||||||
cout << "Multiplying the first 3 elements of the vector v\n " | ||||||||
<< "by the factor -2 gives:\n vmod1 = ( " ; | ||||||||
for ( Iter = v.begin( ) ; Iter != v.end( ) ; Iter++ ) | ||||||||
BrandonPacewic marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
cout << *Iter << " "; | ||||||||
cout << ")." << endl; | ||||||||
|
||||||||
// Using for_each_n to multiply the next 3 elements by a Factor, | ||||||||
// starting at the position saved by the previous for_each_n | ||||||||
for_each_n ( pos, 4, MultValue<int> ( -3 ) ); | ||||||||
|
||||||||
cout << "Multiplying the next 4 elements of the vector v\n " | ||||||||
<< "by the factor -3 gives:\n vmod2 = ( " ; | ||||||||
for ( Iter = v.begin( ) ; Iter != v.end( ) ; Iter++ ) | ||||||||
cout << *Iter << " "; | ||||||||
cout << ")." << endl; | ||||||||
BrandonPacewic marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
} | ||||||||
``` | ||||||||
|
||||||||
```Output | ||||||||
Original vector v1 = ( -4 -3 -2 -1 0 1 2 ). | ||||||||
Multiplying the first 3 elements of the vector v1 | ||||||||
by the factor -2 gives: | ||||||||
v1mod1 = ( 8 6 4 -1 0 1 2 ). | ||||||||
Multiplying the next 4 elements of the vector v1 | ||||||||
by the factor -3 gives: | ||||||||
v1mod2 = ( 8 6 4 3 0 -3 -6 ). | ||||||||
``` | ||||||||
|
||||||||
## <a name="generate"></a> `generate` | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, our style is to not have the extra spacing between parens.