のすべての要素を追加するための次のコードを検討してくださいvector
。
#include<iostream>
#include<algorithm>
#include<numeric>
#include<vector>
using namespace std;
int main(void)
{
std::vector<double> V;
V.push_back(1.2);
V.push_back(3.6);
V.push_back(5.6);
double sum = accumulate(V.begin(),V.end(),0);
cout << "The sum of the elements of the vector V is " << sum << endl;
return 0;
}
これをWindowsのCygwinでコンパイルして実行すると、ターミナルで次のように出力されます。
ベクトルVの要素の合計は9です。
accumulate
関数は、答えを説明するだろうこれは、すべての数字を切り捨てし、それらを加算しているようです。
CygwinのG ++コンパイラでこの何かを間違っては、またはの私の誤解accumulate
加算する機能vector
のdouble
秒?
std::accumulate
そのように宣言されています:
template <typename InputIt, typename T>
T accumulate(InputIt first, InputIt last, T init);
の2番目のテンプレート引数は、が型であるためとstd::accumulate
推定されます。代わりに、のようにdoubleを渡します。int
0
int
0.0
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加