vectorã¯ã©ã¹ãlistã¯ã©ã¹ãªã©ã®STLã³ã³ããã§æ°å¤ãªã¹ãã®åãç©ãã¨ãããå ´å、numericã®accumulateã使ã。ç©ãªã©ã®é¢æ°ãªãã¸ã§ã¯ããå©ç¨ããå ´åã¯functionalã使ã。äºä¹ã®åã®æ±ãæ¹ã示ã。 #include <iostream> #include <vector> #include <numeric> #include <functional> using namespace std; // äºä¹ã®åã®é¢æ°ãªãã¸ã§ã¯ã. template<class T> class add_square : public binary_function<T, T, T> { public: result_type operator()(first_argument_type a, second_argument_type b) { return a + b * b; } }; int main() { vector<int> v(5, 3); // æ°å¤3ã5ã¤å
¥ã£ãvectorã¯ã©ã¹. // å. cout << "3 + 3 + 3 + 3 + 3 = "; cout << accumulate(v.begin(), v.end(), 0) << endl; // ç©. cout << "3 * 3 * 3 * 3 * 3 = "; cout << accumulate(v.begin(), v.end(), 1, multiplies<int>()) << endl; // äºä¹ã®å. cout << "3^2 + 3^2 + 3^2 + 3^2 + 3^2 = "; cout << accumulate(v.begin(), v.end(), 0, add_square<int>()) << endl; return 0; } 以ä¸ã®ããã«è¡¨...