fork download
  1. #include <iostream>
  2. #include <numeric>
  3. #include <iterator>
  4. #include <vector>
  5. #include <functional>
  6.  
  7.  
  8. template<typename Container>
  9. std::string contents_as_string(Container const & c,
  10. std::string const & separator) {
  11. if (c.size() == 0) return "";
  12. auto fold_operation = [&separator] (std::string const & accum,
  13. auto const & item) {
  14. return accum + separator + std::to_string(item);};
  15. return std::accumulate(std::next(std::begin(c)), std::end(c),
  16. std::to_string(*std::begin(c)), fold_operation);
  17. }
  18.  
  19. int main() {
  20. std::vector<double> v(4);
  21. std::iota(std::begin(v), std::end(v), 0.1);
  22. std::cout << contents_as_string(v, ", ") << std::endl;
  23.  
  24. std::vector<int> w(5);
  25. std::iota(std::begin(w), std::end(w), 1);
  26. std::cout << contents_as_string(w, " x ") << " = "
  27. << std::accumulate(std::begin(w), std::end(w), 1, std::multiplies<int>{})
  28. << std::endl;
  29. }
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
0.100000, 1.100000, 2.100000, 3.100000
1 x 2 x 3 x 4 x 5 = 120