名前空間
変種
操作

std::generate_n

提供: cppreference.com
< cpp‎ | algorithm

 
 
アルゴリズムライブラリ
機能します
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
シーケンス動作を非改変
Original:
Non-modifying sequence operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
シーケンス動作を変更する
Original:
Modifying sequence operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
操作を仕切る
Original:
Partitioning operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
(ソートされた範囲で)ソート操作
Original:
Sorting operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
バイナリ検索操作(ソート範囲で)
Original:
Binary search operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
(ソートされた範囲で)操作を設定します
Original:
Set operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
ヒープ操作
Original:
Heap operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
最小値/最大値操作
Original:
Minimum/maximum operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
数値演算
Original:
Numeric operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Cライブラリ
Original:
C library
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
 
Defined in header <algorithm>
template< class OutputIt, class Size, class Generator >

void generate_n( OutputIt first, Size count, Generator g );
template< class OutputIt, class Size, class Generator >

OutputIt generate_n( OutputIt first, Size count, Generator g );
(C++11以前)

(C++11およびそれ以降)
g場合、countで範囲の先頭で最初first要素に、指定された関数オブジェクトcount>0によって生成された値を、代入します。そうでなければ何もしません.
Original:
Assigns values, generated by given function object g, to the first count elements in the range beginning at first, if count>0. Does nothing otherwise.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

目次

[編集] パラメータ

first -
生成する要素の範囲の先頭
Original:
the beginning of the range of elements to generate
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
count -
生成する要素の数
Original:
number of the elements to generate
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
g - generator function object that will be called.

The signature of the function should be equivalent to the following:

Ret fun();

The type  Ret must be such that an object of type OutputIt can be dereferenced and assigned a value of type  Ret. ​

Type requirements
-
OutputIt must meet the requirements of OutputIterator.

[編集] 値を返します

(なし)(C++11以前)
Original:
(none) (C++11以前)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
count>0firstそうでない場合、割り当てられた最後の要素の反復子1。 (C++11およびそれ以降)
Original:
Iterator one past the last element assigned if count>0, first otherwise. (C++11およびそれ以降)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集] 複雑

countg()および割り当ての正確count>0呼び出し、.
Original:
Exactly count invocations of g() and assignments, for count>0.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集] 可能な実装

template< class OutputIt, class Size, class Generator >
OutputIt generate_n( OutputIt first, Size count, Generator g )
{
    for( Size i = 0; i < count; i++ ) {
        *first++ = g();
    }
    return first;
}

[編集]

次のコードは、乱数を整数の配列を埋め.
Original:
The following code fills an array of integers with random numbers.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <cstddef>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <algorithm>
 
int main()
{
    const std::size_t N = 5;
    int ar[N];
    std::generate_n(ar, N, std::rand); // Using the C function rand()
 
    std::cout << "ar: ";
    std::copy(ar, ar+N, std::ostream_iterator<int>(std::cout, " "));
    std::cout << "\n";
}

Output:

52894 15984720 41513563 41346135 51451456

[編集] も参照してください

要素の数に値を代入します
Original:
assigns a value to a number of elements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(関数テンプレート) [edit]
範囲内の関数の結果を保存します
Original:
saves the result of a function in a range
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(関数テンプレート) [edit]