Created
April 29, 2020 16:43
-
-
Save misterpoloy/3626df9ee19a69c0b512e062130cb7ba to your computer and use it in GitHub Desktop.
Sum each element of an array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <any> | |
#include <vector> | |
#include <typeinfo> | |
using namespace std; | |
int productSum(vector<any> array, int multiplier = 1) { | |
int result = 0; | |
for (auto element : array) { | |
if (element.type() == typeid(vector<any>)) { | |
result += productSum(any_cast<vector<any>>(element), multiplier + 1); | |
} else { | |
result += any_cast<int>(element); | |
} | |
} | |
return result * multiplier; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.