Tell me, is there such a function for the vector of integers?
|
1 answer
Yeah almost. There is a function that can fold. It needs to pass two additional parameters - the initial value (in the case of multiplication it is 1, this is logical) and what to do with the elements (multiply). Here is the code
#include <iostream> // std::cout #include <functional> // std::multiplies #include <numeric> // std::accumulate #include <vector> int main () { int init = 1; std::vector<int> numbers = {10,20,30}; std::cout << std::accumulate( numbers.begin(), // Π½Π°ΡΠ°Π»ΠΎ Π΄Π»Ρ ΡΠΌΠ½ΠΎΠΆΠ΅Π½ΠΈΡ numbers.end(),// ΠΊΠΎΠ½Π΅Ρ Π΄Π»Ρ ΡΠΌΠ½ΠΎΠΆΠ΅Π½ΠΈΡ init,// Π½Π°ΡΠ°Π»ΡΠ½ΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ std::multiplies<int>());// Π³ΠΎΡΠΎΠ²Π°Ρ ΡΡΠ½ΠΊΡΠΈΡ ΡΠΌΠ½ΠΎΠΆΠ΅Π½ΠΈΡ. std::cout << '\n'; return 0; } - Thanks, @KoVadim, you are a direct wizard. ____ Strange thing, my accumulate function is not in std space. ) Oh, those standards. - perfect
- Apparently you have a bad compiler. Accidentally not in tr1 namespace? - KoVadim
- She is in a global space. And the standard microsoft compiler from mvs 2010. - perfect
|