function template
<algorithm>
std::generate_ntemplate <class OutputIterator, class Size, class Generator> void generate_n (OutputIterator first, Size n, Generator gen);
template <class OutputIterator, class Size, class Generator> OutputIterator generate_n (OutputIterator first, Size n, Generator gen);
Generate values for sequence with function
Assigns the value returned by successive calls to gen to the first n elements of the sequence pointed by first.The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
template <class OutputIterator, class Size, class Generator>
void generate_n ( OutputIterator first, Size n, Generator gen )
{
while (n>0) {
*first = gen();
++first; --n;
}
}
This value shall not be negative.
If negative, the function does nothing.
none
An iterator pointing to the element that follows the last element whose value has been generated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// generate_n example
#include <iostream> // std::cout
#include <algorithm> // std::generate_n
int current = 0;
int UniqueNumber () { return ++current; }
int main () {
int myarray[9];
std::generate_n (myarray, 9, UniqueNumber);
std::cout << "myarray contains:";
for (int i=0; i<9; ++i)
std::cout << ' ' << myarray[i];
std::cout << '\n';
return 0;
}
myarray contains: 1 2 3 4 5 6 7 8 9
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