Skip to content

Instantly share code, notes, and snippets.

@misterpoloy
Created April 29, 2020 16:43
Show Gist options
  • Save misterpoloy/3626df9ee19a69c0b512e062130cb7ba to your computer and use it in GitHub Desktop.
Save misterpoloy/3626df9ee19a69c0b512e062130cb7ba to your computer and use it in GitHub Desktop.
Sum each element of an array
#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;
}
@misterpoloy
Copy link
Author

misterpoloy commented Apr 29, 2020

ProductSum

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment