generate
Replaces each element with the result of an operation
Key Facts
Gyroscopic Couple: The rate of change of angular momentum () = (In the limit).- = Moment of Inertia.
- = Angular velocity
- = Angular velocity of precession.
Blaise Pascal (1623-1662) was a French mathematician, physicist, inventor, writer and Catholic philosopher.
Leonhard Euler (1707-1783) was a pioneering Swiss mathematician and physicist.
Definition
The generate() algorithm is defined in the standard header <algorithm> and in the nonstandard backward-compatibility header <algo.h>.Interface
#include <algorithm> template < class ForwardIterator, class Generator > void generate( ForwardIterator first, ForwardIterator last, Generator gen );Parameters:
Parameter | Description |
---|---|
first | A forward iterator addressing the position of the first element in the range to which values are to be assigned |
last | A forward iterator addressing the position one past the final element in the range to which values are to be assigned |
gen | A function object that is called with no arguments that is used to generate the values to be assigned to each of the elements in the range |
References
Example:
Example - generate, rand functions
Problem
This program illustrates the use of the STL generate() algorithm to fill a range of values in a vector of integers with values generated by the rand() function from <cstdlib>.
Workings
#include <iostream> #include <algorithm> #include <vector> #include <cstdlib> using namespace std; int main() { vector<int> v(10); cout <<"\nHere are the initial contents of v:\n"; for (vector<int>::size_type i=0; i<v.size(); i++) cout <<v.at(i)<<" "; cout <<"\nNow we fill v with randomly generated integer values."; generate(v.begin(), v.end(), rand); cout <<"\nHere are the revised contents of v:\n"; for (vector<int>::size_type i=0; i<v.size(); i++) cout <<v.at(i)<<" "; cout <<"\nFinally, we fill the last half of v with new randomly generated integer values."; generate(v.begin()+5, v.end(), rand); cout <<"\nHere are the revised contents of v:\n"; for (vector<int>::size_type i=0; i<v.size(); i++) cout <<v.at(i)<<" "; return 0; }
Solution
Output:
Here are the initial contents of v:
0 0 0 0 0 0 0 0 0 0 Now we fill v with randomly generated integer values.
Here are the revised contents of v:
41 18467 6334 26500 19169 15724 11478 29358 26962 24464 Finally, we fill the last half of v with new randomly generated integer values.
Here are the revised contents of v:
41 18467 6334 26500 19169 5705 28145 23281 16827 9961
0 0 0 0 0 0 0 0 0 0 Now we fill v with randomly generated integer values.
Here are the revised contents of v:
41 18467 6334 26500 19169 15724 11478 29358 26962 24464 Finally, we fill the last half of v with new randomly generated integer values.
Here are the revised contents of v:
41 18467 6334 26500 19169 5705 28145 23281 16827 9961
References