std::generate_n
提供: cppreference.com
![]() | このページは、Google 翻訳を使って英語版から機械翻訳されました。 翻訳には誤りや奇妙な言い回しがあるかもしれません。文章の上にポインタをおくと、元の文章が見れます。誤りを修正して翻訳を改善する手助けをしてください。翻訳についての説明は、ここをクリックしてください。 |
Defined in header <algorithm> | ||
template<class OutputIt, class Size, class Generator > void 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.
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:
The type Ret must be such that an object of type OutputIt can be dereferenced and assigned a value of type Ret. | |||||||||
型の要件 | |||||||||||
-OutputIt は 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.
You can help to correct and verify the translation. Click here for instructions.
count>0
、first
そうでない場合、割り当てられた最後の要素の反復子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.
You can help to correct and verify the translation. Click here for instructions.
[編集]複雑性
count
用g()
および割り当ての正確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.
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.
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(){conststd::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";}
出力:
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. (関数テンプレート) | |
範囲内の関数の結果を保存します 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. (関数テンプレート) |