int main(int argc, char* argv[]) { vector<int> v(10,5); // 10 copies of 5 // sum of all elements of v Sum<int> s; s = for_each(v.begin(), v.end(), s); cout << "the sum is " << s.result() << '\n'; return 0; }
void f(list<double>& ls) { Sum<double> sum; sum = for_each(ls.begin((), ls.end(), sum); // 对表中每一个元素调用sum()函数对象 cout << "the sum is " << sum.result() << endl; }