C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C++ Algorithm generate_n()C++ Algorithm generate_n() function is used to assign the values which is generated by a function object to a specified number of elements in a range and returns to the one past the last assigned value position. The generator function is defined by the user and it is called successively for assigning the numbers. Syntaxtemplate <class OutputIterator, class Size, class Generator> void generate_n (OutputIterator first, Size n, Generator gen); //Until C++ 11 template <class OutputIterator, class Size, class Generator> OutputIterator generate_n (OutputIterator first, Size n, Generator gen); //Since C++ 11 Parameterfirst: A forward iterator pointing the position of the first element in the range to which values are to be assigned. gen: A function object with no arguments that is used to generate the values to be assigned to each of the elements in the range. n: Number of elements to be assigned by generator function. It may be signed or unsigned integer type. Return valueNone ComplexityComplexity is linear in n. It calls gen and performs an assignment for each element. Data racesThe first n object in the range pointed by first are modified where each object is modified exactly once. Exception safetyThis function throws an exception if any of gen, element assignments or the operation on an iterator throws an exception. Please note that invalid parameters cause an undefined behavior. Example 1Let's see the simple example to demonstrate the use of generate_n(): #include <algorithm> #include <iostream> #include <iterator> using namespace std; int main() { int n = 1; generate_n(ostream_iterator<int>(cout, ","), 10, [&n]{ auto t = n; n *= 2; return t; }); return 0; } Output: 1,2,4,8,16,32,64,128,256,512, Example 2Let's see another simple example: #include <vector> #include <deque> #include <algorithm> #include <iostream> #include <ostream> using namespace std; int main() { // Assigning random values to vector integer elements vector <int> v1 ( 5 ); vector <int>::iterator Iter1; deque <int> deq1 ( 5 ); deque <int>::iterator d1_Iter; generate_n ( v1.begin ( ), 3 , rand ); cout <<"Vector v1 is ( " ; for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ ) cout <<*Iter1<<" "; cout <<")."<<endl; // Assigning random values to deque integer elements generate_n ( deq1.begin ( ), 4 , rand ); cout <<"Deque deq1 is ( " ; for ( d1_Iter = deq1.begin( ) ; d1_Iter != deq1.end( ) ; d1_Iter++ ) cout <<*d1_Iter<<" "; cout <<")."<<endl; return 0; } Output: Vector v1 is ( 1804289383 846930886 1681692777 0 0 ). Deque deq1 is ( 1714636915 1957747793 424238335 719885386 0 ). Example 3Let's see another simple example: #include <iostream> #include <vector> #include <algorithm> // Defining the generator function int gen() { static int i = 0; return ++i; } using namespace std; int main() { int i; // Declaring a vector of size 10 vector<int> v1(10); // using std::generate_n std::generate_n(v1.begin(), 10, gen); vector<int>::iterator i1; for (i1 = v1.begin(); i1 != v1.end(); ++i1) { cout << *i1 << " "; } return 0; } Output: 1 2 3 4 5 6 7 8 9 10 Example 4Let's see another simple example: #include <iostream> // std::cout #include <algorithm> // std::generate_n using namespace std; int current = 0; int UniqueNumber () { return ++current; } int main () { int myarray[9]; generate_n (myarray, 9, UniqueNumber); cout << "myarray contains:"; for (int i=0; i<9; ++i) cout << ' ' << myarray[i]; cout << '\n'; return 0; } Output: myarray contains: 1 2 3 4 5 6 7 8 9
Next TopicC++ Algorithm
|