Skip to content

Instantly share code, notes, and snippets.

@Dobiasd
Created November 17, 2017 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dobiasd/923ba25dac7c62eb6f5b432de6d7586b to your computer and use it in GitHub Desktop.
Save Dobiasd/923ba25dac7c62eb6f5b432de6d7586b to your computer and use it in GitHub Desktop.
#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
int main()
{
typedef std::map<int, std::string> IntStringMap;
IntStringMap divisorsWithWords;
divisorsWithWords[3] = "Fizz";
divisorsWithWords[5] = "Buzz";
for(int i(1); i <= 100; ++i)
{
std::string word =
std::accumulate(std::begin(divisorsWithWords),
std::end(divisorsWithWords), std::string(),
[&](const std::string& acc,
const IntStringMap::value_type& elem)
{
if(i % elem.first == 0)
return acc + elem.second;
return acc;
});
if(word.empty())
word = std::to_string(i);
std::cout << word << ", ";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment