C ++のtransform_inclusive_scan()関数
このチュートリアルでは、C ++のtransform_inclusive_scan()関数を理解するためのプログラムについて説明します。
例
#include <iostream>
#include <vector>
using namespace std;
namespace point_input_iterator {
template <class InputItrator, class OutputItrator, class BinaryOperation, class UnaryOperation>
OutputItrator transform_inclusive_scan(InputItrator first,
InputItrator last,
OutputItrator d_first,
BinaryOperation binary_op,
UnaryOperation unary_op){
*d_first = unary_op(*first);
first++;
d_first++;
for (auto it = first; it != last; it++) {
//calculating the prefix sum
*d_first = binary_op(unary_op(*it), *(d_first - 1));
d_first++;
}
return d_first;
}
}
int main(){
//inputting elements using vector
vector<int> InputVector{ 11, 22, 33, 44, 55, 66, 77, 88 };
vector<int> OutputVector(8);
point_input_iterator::transform_inclusive_scan(InputVector.begin(), InputVector.end(), OutputVector.begin(), [](auto xx, auto yy) {
return xx + yy;
},
[](auto xx) {
return xx * xx;
});
for (auto item : OutputVector) {
//printing the output item
cout << item << " ";
}
cout << std::endl;
return 0;
} 出力
121 605 1694 3630 6655 11011 16940 24684
-
C ++のlog()関数
C / C++ライブラリ関数doublelog(double x)は、xの自然対数(baseelogarithm)を返します。以下はlog()関数の宣言です。 double log(double x) パラメータは浮動小数点値です。そして、この関数はxの自然対数を返します。 例 #include <iostream> #include <cmath> using namespace std; int main () { double x, ret; x = 2.7; /* finding l
-
C ++のswap()関数
swap()関数は、2つの数値を交換するために使用されます。この関数を使用すると、2つの数値を交換するために3番目の変数は必要ありません。 C ++言語でのswap()の構文は次のとおりです。 void swap(int variable_name1, int variable_name2); 変数に値を割り当てるか、ユーザー定義の値を渡すと、変数の値が交換されますが、変数の値は実際の場所では同じままです。 これがC++言語でのswap()の例です 例 #include <bits/stdc++.h> using namespace std; int main() { &nb