The Wayback Machine - https://web.archive.org/web/20180426222629/http://ja.cppreference.com:80/w/cpp/algorithm/transform
名前空間
変種
操作

std::transform

提供: cppreference.com
< cpp‎ | algorithm

 
 
アルゴリズムライブラリ
実行ポリシー (C++17)
非変更シーケンス操作
(C++11)(C++11)(C++11)
(C++17)
変更シーケンス操作
(C++11)
(C++11)
transform

未初期化記憶域の操作
分割操作
ソート操作
バイナリサーチ操作
集合操作 (ソート済み範囲に対する)
ヒープ操作
(C++11)
最小/最大演算
(C++11)
(C++17)
順列
数値演算
C のライブラリ
 
ヘッダ <algorithm> で定義
template<class InputIt, class OutputIt, class UnaryOperation >

OutputIt transform( InputIt first1, InputIt last1, OutputIt d_first,

                    UnaryOperation unary_op );
(1)
template<class InputIt1, class InputIt2, class OutputIt, class BinaryOperation >

OutputIt transform( InputIt1 first1, InputIt1 last1, InputIt2 first2,

                    OutputIt d_first, BinaryOperation binary_op );
(2)
std::transform範囲に指定した関数を適用し、d_firstから始まる別の範囲に結果を格納.
Original:
std::transform applies the given function to a range and stores the result in another range, beginning at d_first.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
最初のバージョンでは単項演算unary_op[first1, last1)によって定義された範囲に適用されます。第二版ではバイナリ操作binary_opは二つの範囲の要素のペアに適用されます。[first1, last1)first2や他の初めによって定義された1.
Original:
In the first version unary operation unary_op is applied to the range defined by [first1, last1). In the second version the binary operation binary_op is applied to pairs of elements from two ranges: one defined by [first1, last1) and the other beginning at first2.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

目次

[編集]パラメータ

first1, last1 -
変換する要素の最初の範囲
Original:
the first range of elements to transform
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
first2 -
変換する要素の第2の範囲の始まり
Original:
the beginning of the second range of elements to transform
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
d_first -
目的の範囲の始まりは、first1またはfirst2に等しいかもしれません
Original:
the beginning of the destination range, may be equal to first1 or first2
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
unary_op - unary operation function object that will be applied.

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

 Ret fun(const Type &a);

The signature does not need to have const&.
Type は型 InputIt のオブジェクトの逆参照から暗黙に変換可能なものでなければなりません。 The type Ret must be such that an object of type OutputIt can be dereferenced and assigned a value of type Ret. ​

binary_op - binary operation function object that will be applied.

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

 Ret fun(const Type1 &a, const Type2 &b);

The signature does not need to have const&.
Type1 および Type2 は、 InputIt1 および InputIt2 型のオブジェクトの逆参照からType1 および Type2 にそれぞれ暗黙に変換可能なものでなければなりません。 The type Ret must be such that an object of type OutputIt can be dereferenced and assigned a value of type Ret. ​

型の要件
-
InputItInputIterator の要件を満たさなければなりません。
-
InputIt1InputIterator の要件を満たさなければなりません。
-
InputIt2InputIterator の要件を満たさなければなりません。
-
OutputItOutputIterator の要件を満たさなければなりません。

[編集]値を返します

最後の要素は、過去の要素への出力イテレータは、形質転換された.
Original:
output iterator to the element past the last element transformed.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集]複雑性

1)
std::distance(first1, last1)の正確unary_opアプリケーション
Original:
exactly std::distance(first1, last1) applications of unary_op
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
std::distance(first1, last1)の正確binary_opアプリケーション
Original:
exactly std::distance(first1, last1) applications of binary_op
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集]要件

unary_opbinary_op全く副作用がありません。 (C++11以前)
Original:
unary_op and binary_op have no side effects. (C++11以前)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
unary_opbinary_op終了イテレータを含む任意のイテレータを無効、または関与の範囲の任意の要素を変更しません。 (C++11およびそれ以降)
Original:
unary_op and binary_op do not invalidate any iterators, including the end iterators, or modify any elements of the ranges involved. (C++11およびそれ以降)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
これらの要件の目的は、並列またはstd::transformのアウトオブオーダーの実装を可能にすることである。順序でシーケンスに関数を適用するには、std::for_eachを使用.
Original:
The intent of these requirements is to allow parallel or out-of-order implementations of std::transform. To apply a function to a sequence in-order, use std::for_each.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集]可能な実装

1つめのバージョン
template<class InputIt, class OutputIt, class UnaryOperation> OutputIt transform(InputIt first1, InputIt last1, OutputIt d_first, UnaryOperation unary_op){while(first1 != last1){*d_first++= unary_op(*first1++);}return d_first;}
2つめのバージョン
template<class InputIt1, class InputIt2, class OutputIt, class BinaryOperation> OutputIt transform(InputIt first1, InputIt last1, InputIt first2, OutputIt d_first, BinaryOperation binary_op){while(first1 != last1){*d_first++= binary_op(*first1++, *first2++);}return d_first;}

[編集]

次のコードは、toupperは関数を使って文字列を大文字に変換するために変換を使用しています
Original:
The following code uses transform to convert a string to uppercase using the toupper function:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <string>#include <cctype>#include <algorithm>#include <iostream>   int main(){std::string s("hello"); std::transform(s.begin(), s.end(), s.begin(), (int(*)(int))std::toupper);std::cout<< s;}

出力:

HELLO

[編集]参照

指定範囲の要素に関数を適用します
(関数テンプレート)[edit]
close