@@ -2191,7 +2191,7 @@ int main()
21912191
21922192 // The function object is templatized and so can be
21932193 // used again on the elements with a different Factor
2194- for_each (v1.begin( ), v1.end( ), MultValue<int> (5 ) );
2194+ for_each ( v1.begin( ), v1.end( ), MultValue<int> ( 5 ) );
21952195
21962196 cout << "Multiplying the elements of the vector v1mod\n "
21972197 << "by the factor 5 gives:\n v1mod2 = ( " ;
@@ -2223,19 +2223,130 @@ Average ( v1mod2 ) = 10.
22232223
22242224## <a name="for_each_n"></a> `for_each_n`
22252225
2226+ Applies a specified function object to a specified number of elements in a range beginning with a particular element.
2227+
22262228```cpp
22272229template<class InputIterator, class Size, class Function>
22282230InputIterator for_each_n(
22292231 InputIterator first,
2230- Size n ,
2231- Function f );
2232+ Size count ,
2233+ Function func );
22322234
22332235template<class ExecutionPolicy, class ForwardIterator, class Size, class Function>
22342236ForwardIterator for_each_n(
22352237 ExecutionPolicy&& exec,
22362238 ForwardIterator first,
2237- Size n,
2238- Function f);
2239+ Size count,
2240+ Function func);
2241+ ```
2242+
2243+ ### Parameters
2244+
2245+ *`exec`*\
2246+ The execution policy to use.
2247+
2248+ *`first`*\
2249+ An input iterator addressing the position of the first element in the range to be operated on.
2250+
2251+ *`count`*\
2252+ A signed or unsigned integral type specifying the number of elements to be operated on.
2253+
2254+ *`func`*\
2255+ User-defined function object that is applied to each element in the range [`first`, `first` + `count`).
2256+
2257+ ### Return value
2258+
2259+ An iterator to the element that follows the last element processed if *`count`* > zero,
2260+ otherwise the first element.
2261+
2262+ ### Remarks
2263+
2264+ *`count`* must be non-negative, and there must be at least *`count`* elements in the range starting at *`first`*.
2265+
2266+ ### Example
2267+
2268+ This example depicts a user-defined function object class. Production code often
2269+ uses [`lambda`](https://docs.microsoft.com/cpp/cpp/lambda-expressions-in-cpp?view=msvc-170)s to achieve the same result with less code.
2270+
2271+ ```cpp
2272+ // alg_for_each_n.cpp
2273+ // compile with /EHsc and /std:c++17 (or higher)
2274+ #include <algorithm>
2275+ #include <iostream>
2276+ #include <vector>
2277+ using namespace std; // Global standard namespace (bad practice)
2278+
2279+ // The function object multiplies an element by a Factor
2280+ template <class Type> class MultValue
2281+ {
2282+ private:
2283+ Type Factor; // The value to multiply by
2284+ public:
2285+ // Constructor initializes the value to multiply by
2286+ MultValue(const Type &value) : Factor(value)
2287+ {
2288+ }
2289+
2290+ // The function call for the element to be multiplied
2291+ void operator()(Type &elem) const
2292+ {
2293+ elem *= Factor;
2294+ }
2295+ };
2296+
2297+ template <class T> void print_vector(const vector<T> &vec)
2298+ {
2299+ cout << "( ";
2300+
2301+ for (auto iter = vec.begin(); iter != vec.end(); iter++)
2302+ {
2303+ std::cout << *iter << ' ';
2304+ }
2305+
2306+ cout << ")." << endl;
2307+ }
2308+
2309+ int main()
2310+ {
2311+ vector<int> v;
2312+
2313+ // Constructing vector v
2314+ for (int i = -4; i <= 2; i++)
2315+ {
2316+ v.push_back(i);
2317+ }
2318+
2319+ cout << "Original vector v = ";
2320+ print_vector(v);
2321+
2322+ // Using for_each_n to multiply the first 3 elements by a Factor,
2323+ // saving position
2324+ auto pos = for_each_n(v.begin(), 3, MultValue<int>(-2));
2325+
2326+ cout << "Multiplying the first 3 elements of the vector v\n "
2327+ << "by the factor -2 gives:\n vmod1 = ";
2328+ print_vector(v);
2329+
2330+ // Using for_each_n to multiply the next 4 elements by a Factor,
2331+ // starting at the position saved by the previous for_each_n
2332+ for_each_n(pos, 4, MultValue<int>(-3));
2333+
2334+ cout << "Multiplying the next 4 elements of the vector v\n "
2335+ << "by the factor -3 gives:\n vmod2 = ";
2336+ print_vector(v);
2337+
2338+ return 0;
2339+ }
2340+ ```
2341+
2342+ ```Output
2343+ Original vector v = ( -4 -3 -2 -1 0 1 2 ).
2344+ Multiplying the first 3 elements of the vector v
2345+ by the factor -2 gives:
2346+ vmod1 = ( 8 6 4 -1 0 1 2 ).
2347+ Multiplying the next 4 elements of the vector v
2348+ by the factor -3 gives:
2349+ vmod2 = ( 8 6 4 3 0 -3 -6 ).
22392350```
22402351
22412352## <a name="generate"></a> `generate`
0 commit comments