From f78a92ef747c9c476d41d8093711405783941947 Mon Sep 17 00:00:00 2001 From: Samuel Fadel Date: Wed, 1 Aug 2018 20:07:09 -0300 Subject: More iterators; less explicit types; removed loops. --- src/main.cpp | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/main.cpp b/src/main.cpp index 8291db2..446e456 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,30 +8,28 @@ const char *TICKS[] = {"▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"}; const size_t NUM_TICKS = 8; const double EPSILON = 1e-8; -template -void sparkify(const std::vector &values, std::ostream &out) { - auto minmax = std::minmax_element(values.begin(), values.end()); +template +void sparkify(InputIterator first, InputIterator last, OutputIterator &out) { + auto minmax = std::minmax_element(first, last); auto min = *minmax.first, max = *minmax.second; auto values_range = max - min; if (abs(values_range) < EPSILON) { - auto it = std::ostream_iterator(out); - std::fill_n(it, values.size(), TICKS[0]); + std::fill_n(out, last - first, TICKS[0]); } else { auto coef = (NUM_TICKS - 1) / values_range; - for (const T &x: values) { - out << TICKS[(size_t) round((x - min) * coef)]; - } + std::transform(first, last, out, [&](auto x) { + return TICKS[(size_t) round((x - min) * coef)]; + }); } } int main() { - std::vector values; - for (double value; std::cin >> value;) { - values.push_back(value); - } + std::istream_iterator cin_it(std::cin), eos; + std::vector values(cin_it, eos); + if (!std::cin.eof() && std::cin.fail()) { std::cerr << "Parse error" << std::endl; return 1; @@ -40,7 +38,8 @@ int main() return 0; } - sparkify(values, std::cout); + std::ostream_iterator cout_it(std::cout); + sparkify(values.begin(), values.end(), cout_it); std::cout << std::endl; return 0; } -- cgit v1.2.3