TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ algorithm generate() function

C++ algorithm generate() function with c++ tutorial for beginners and professionals with examples on adjacent_find(),any_of(), copy(), copy_if(), count(), count_if(), equal(), find(), find_end(), find_first_of(), find_if(), find_if_not(), for_each() etc.

<< Back to CPP

C++ Algorithm generate ()

C++ Algorithm generate() function is used to assign the value generated by a function object to each element in a range.

The generator function is defined by the user and it is called successively for assigning the numbers.

Syntax

template <class ForwardIterator, class Generator>
  void generate (ForwardIterator first, ForwardIterator last, Generator gen);

Parameter

first: A forward iterator pointing the position of the first element in the range to which values are to be assigned.

last: A forward iterator pointing the position one past the final 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.

Return value

None

Complexity

Complexity is linear in the range [first, last). It calls gen and performs an assignment for each element.

Data races

The object in the range [first ,last) are modified. Each object is accessed exactly once.

Exception safety

This function throws an exception if the any of gen, element assignments or the operation on an iterator throws an exception.

Please note that invalid parameters cause an undefined behavior.

Example 1

Let's see the simple example to demonstrate the use of generate ():

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  vector<int> v(10);

  int n = 1;
  generate(v.begin(), v.end(), [&n]() { 
      auto t = n; 
      n *= 2; 
      return t; 
      } );

  for_each(v.begin(), v.end(), [](int x) { 
      cout << x << ","; 
      });
  
  return 0;
}

Output:

1,2,4,8,16,32,64,128,256,512,

Example 2

Let'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 
    std::generate(v1.begin(), v1.end(), 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 3

Let's see another simple example:

#include <vector>  
#include <deque>  
#include <algorithm>  
#include <iostream>  
#include <ostream>  
  
int main( )  
{  
   using namespace std;  
  
   // 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 ( v1.begin ( ), v1.end ( ) , 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 ( deq1.begin ( ), deq1.end ( ) , 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 1714636915 1957747793 ).
Deque deq1 is ( 424238335 719885386 1649760492 596516649 1189641421 ).

Example 4

Let's see another simple example:

#include <iostream>     // std::cout
#include <algorithm>    // std::generate
#include <vector>       // std::vector
#include <ctime>        // std::time
#include <cstdlib>      // std::rand, std::srand

using namespace std;

// function generator:
int RandomNumber () { return (rand()%100); }

// class generator:
struct c_unique {
  int current;
  c_unique() {current=0;}
  int operator()() {return ++current;}
} UniqueNumber;

int main () {
  srand ( unsigned ( std::time(0) ) );

  vector<int> myvector (8);

  generate (myvector.begin(), myvector.end(), RandomNumber);

  cout << "myvector contains:";
  for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
    cout << ' ' << *it;
  cout << '\n';

  generate (myvector.begin(), myvector.end(), UniqueNumber);

  cout << "myvector contains:";
  for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
    cout << ' ' << *it;
   cout << '\n';
 
  return 0;
}

Output:

myvector contains: 93 16 77 25 39 52 56 19
myvector contains: 1 2 3 4 5 6 7 8

Next TopicC++ Algorithm




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf