diff --git a/chapter_06/examples/example_text.txt b/chapter_06/examples/example_text.txt new file mode 100644 index 0000000000000000000000000000000000000000..9cabe0ddc4321fdeba7a76b4dcb46e6fcaf8a6c2 --- /dev/null +++ b/chapter_06/examples/example_text.txt @@ -0,0 +1,23 @@ +A solar flare is a relatively intense, localized emission of +electromagnetic radiation in the Sun's atmosphere. Flares +occur in active regions and are often, but not always, +accompanied by coronal mass ejections, solar particle events, +and other eruptive solar phenomena. The occurrence of solar +flares varies with the 11-year solar cycle. + +Solar flares are thought to occur when stored magnetic energy +in the Sun's atmosphere accelerates charged particles in the +surrounding plasma. This results in the emission of +electromagnetic radiation across the electromagnetic spectrum. + +The extreme ultraviolet and x-ray radiation from solar flares +is absorbed by the daylight side of Earth's upper atmosphere, +in particular the ionosphere, and does not reach the surface. +This absorption can temporarily increase the ionization of +the ionosphere which may interfere with short-wave radio +communication. The prediction of solar flares is an active +area of research. + +Flares also occur on other stars, where the term stellar +flare applies. + diff --git a/chapter_06/examples/word_count.cc b/chapter_06/examples/word_count.cc index 7cdc5cde9d95eff68b36b46fec79cae89a9038e0..3c64d84144e0ad41472ff0d991924425a930c4f5 100644 --- a/chapter_06/examples/word_count.cc +++ b/chapter_06/examples/word_count.cc @@ -1,6 +1,5 @@ #include <fstream> -#include <iomanip> -#include <iostream> +#include <print> #include <map> #include <string> @@ -14,8 +13,6 @@ auto main(int argc, char* argv[]) -> int freq[s]++; for (auto&& [word, count] : freq) - std::cout << std::setw(12) << word - << std::setw(4) << ':' - << std::setw(12) << count - << "\n"; + std::print("{:20} :{:12}\n", + word, count); } diff --git a/chapter_06/examples/word_count_23.cc b/chapter_06/examples/word_count_23.cc new file mode 100644 index 0000000000000000000000000000000000000000..8e83b95ee6778e4235fb032622fc27d899d1b96a --- /dev/null +++ b/chapter_06/examples/word_count_23.cc @@ -0,0 +1,29 @@ +#include <fstream> +#include <format> +#include <ranges> +#include <print> +#include <map> +#include <string> + +namespace sr = std::ranges; +namespace sv = sr::views; + +auto main(int argc, char* argv[]) -> int +{ + std::ifstream fin(argv[1]); + std::map<std::string, unsigned> freq; + std::string s; + auto non_alphabetic = [](auto c) { return not isalpha(c); }; + + while (fin >> s) { + auto s2 = s | sv::drop_while(non_alphabetic) + | sv::reverse + | sv::drop_while(non_alphabetic) + | sv::reverse + | sr::to<std::string>(); + freq[s2]++; + } + + for (auto&& [word, count] : freq) + std::print("{:20} :{:12}\n", word, count); +}