A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/cpp_standard_library/cpp_algorithm_generate.htm below:

C++ Algorithm Library - generate() Function

C++ Algorithm Library - generate() Function Description

The C++ function std::algorithm::generate() assigns the value returned by successive calls to gen to the elements in the range of first to last.

Declaration

Following is the declaration for std::algorithm::generate() function form std::algorithm header.

C++98
template <class ForwardIterator, class Generator>
void generate (ForwardIterator first, ForwardIterator last, Generator gen);
Parameters Return value

None

Exceptions

Throws exception if either gen function or an operation on an iterator throws exception.

Please note that invalid parameters cause undefined behavior.

Time complexity

Linear.

Example

The following example shows the usage of std::algorithm::generate() function.

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

using namespace std;

int main(void) {
   vector<int> v(5);

   generate(v.begin(), v.end(), rand);

   cout << "Vector contains following random numbers" << endl;

   for (auto it = v.begin(); it != v.end(); ++it)
      cout << *it << endl;

   return 0;
}

Let us compile and run the above program, this will produce the following result −

Vector contains following random numbers
1804289383
846930886
1681692777
1714636915
1957747793

algorithm.htm


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4