@@ -2270,23 +2270,23 @@ The complexity is linear with at most *`count`* operations.
22702270```cpp
22712271// alg_for_each_n.cpp
22722272// compile with: /EHsc
2273- #include <vector>
22742273#include <algorithm>
22752274#include <iostream>
2275+ #include <vector>
22762276
22772277// The function object multiplies an element by a Factor
2278- template <class Type>
2279- class MultValue
2278+ template <class Type> class MultValue
22802279{
2281- private:
2282- Type Factor; // The value to multiply by
2283- public:
2280+ private:
2281+ Type Factor; // The value to multiply by
2282+ public:
22842283 // Constructor initializes the value to multiply by
2285- MultValue ( const Type& value ) : Factor ( value ) {
2284+ MultValue(const Type &value) : Factor(value)
2285+ {
22862286 }
22872287
22882288 // The function call for the element to be multiplied
2289- void operator( ) ( Type& elem ) const
2289+ void operator()( Type & elem) const
22902290 {
22912291 elem *= Factor;
22922292 }
@@ -2299,36 +2299,46 @@ int main()
22992299 vector<int>::iterator Iter;
23002300
23012301 // Constructing vector v
2302- int i;
2303- for ( i = -4 ; i <= 2 ; i++ )
2302+ for (int i = -4; i <= 2; i++)
23042303 {
2305- v.push_back( i );
2304+ v.push_back(i );
23062305 }
23072306
2308- cout << "Original vector v = ( " ;
2309- for ( Iter = v.begin( ) ; Iter != v.end( ) ; Iter++ )
2307+ cout << "Original vector v = ( ";
2308+ for (Iter = v.begin(); Iter != v.end(); Iter++)
2309+ {
23102310 cout << *Iter << " ";
2311+ }
2312+
23112313 cout << ")." << endl;
23122314
23132315 // Using for_each_n to multiply the first 3 elements by a Factor,
23142316 // saving position
2315- auto pos = for_each_n ( v.begin( ), 3, MultValue<int> ( -2 ) );
2317+ auto pos = for_each_n( v.begin(), 3, MultValue<int>(-2) );
23162318
23172319 cout << "Multiplying the first 3 elements of the vector v\n "
2318- << "by the factor -2 gives:\n vmod1 = ( " ;
2319- for ( Iter = v.begin( ) ; Iter != v.end( ) ; Iter++ )
2320+ << "by the factor -2 gives:\n vmod1 = ( ";
2321+ for (Iter = v.begin(); Iter != v.end(); Iter++)
2322+ {
23202323 cout << *Iter << " ";
2324+ }
2325+
23212326 cout << ")." << endl;
23222327
23232328 // Using for_each_n to multiply the next 3 elements by a Factor,
23242329 // starting at the position saved by the previous for_each_n
2325- for_each_n ( pos, 4, MultValue<int> ( -3 ) );
2330+ for_each_n( pos, 4, MultValue<int>(-3) );
23262331
23272332 cout << "Multiplying the next 4 elements of the vector v\n "
2328- << "by the factor -3 gives:\n vmod2 = ( " ;
2329- for ( Iter = v.begin( ) ; Iter != v.end( ) ; Iter++ )
2333+ << "by the factor -3 gives:\n vmod2 = ( ";
2334+ for (Iter = v.begin(); Iter != v.end(); Iter++)
2335+ {
23302336 cout << *Iter << " ";
2337+ }
2338+
23312339 cout << ")." << endl;
2340+
2341+ return 0;
23322342}
23332343```
23342344
0 commit comments