我想寫一個計算STL容器中元素總和的泛型函數。我去了解它的方式如下(t
是一個容器):STL容器上的C++模板函數
template <typename T> double Sum(const T& t){
typename T::reverse_iterator rit = t.rbegin();
double dSum = 0.;
while(rit != t.rend()){
dSum += (*rit);
++rit;
}
return dSum;
}
,但我得到了一大堆錯誤。我想這個問題是關於我定義迭代器的第二行嗎?希望得到任何幫助:)
像['標準:: accumulate'(http://en.cppreference.com/w/cpp/algorithm/accumulate)? –
什麼錯誤?廣場至少第一個 –