Last active
March 11, 2025 01:42
-
-
Save fabiogaluppo/60933b541ca5e7630dd2424d9cb2cada to your computer and use it in GitHub Desktop.
istream_helper_util usage samples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Source code by Fabio Galuppo | |
//C++ MasterClass - https://www.linkedin.com/company/cppmasterclass - https://cppmasterclass.com.br/ | |
//Fabio Galuppo - http://member.acm.org/~fabiogaluppo - [email protected] | |
//March 2025 | |
//g++ -O3 -std=c++20 istream_helper_util.t.cpp -o a.exe | |
#include "istream_helper_util.h" | |
#include <algorithm> | |
#include <fstream> | |
#include <functional> | |
#include <iostream> | |
#include <iterator> | |
#include <numeric> | |
#include <ranges> | |
#include <sstream> | |
#include <string> | |
#include <vector> | |
#include <utility> | |
/* | |
void example_1() | |
{ | |
std::cout << __FUNCTION__ << '\n'; | |
while (auto opt = istream_helper::Util::next(std::cin, std::identity{})) | |
std::cout << '\t' << *opt << '\n'; | |
} | |
void example_2() | |
{ | |
std::cout << __FUNCTION__ << '\n'; | |
while (auto opt = istream_helper::Util::next(std::cin)) | |
std::cout << '\t' << *opt << '\n'; | |
} | |
*/ | |
void example_3() | |
{ | |
std::cout << __FUNCTION__ << '\n'; | |
std::ifstream ifs("numbers.txt"); | |
while (auto opt = istream_helper::Util::next(ifs)) | |
std::cout << '\t' << *opt << '\n'; | |
} | |
void example_4() | |
{ | |
std::cout << __FUNCTION__ << '\n'; | |
auto to_ints = [](const std::string& line) { | |
std::istringstream iss{ line }; | |
std::ranges::istream_view<int> isv{ iss }; | |
std::vector<int> temp; | |
std::ranges::copy(isv, std::back_inserter(temp)); | |
return temp; | |
}; | |
std::ifstream ifs("numbers.txt"); | |
while (auto opt = istream_helper::Util::next(ifs, to_ints)) | |
{ | |
std::vector<int>& xs = *opt; | |
std::cout << '\t'; | |
for (int x : xs) std::cout << x << ' '; | |
std::cout << '\n'; | |
} | |
} | |
void example_5() | |
{ | |
std::cout << __FUNCTION__ << '\n'; | |
auto to_pair = [](const std::string& line) { | |
std::istringstream iss{ line }; | |
std::ranges::istream_view<int> isv{ iss }; | |
std::vector<int> temp; std::ranges::copy(isv, std::back_inserter(temp)); | |
return std::make_pair(line, temp);; | |
}; | |
std::ifstream ifs("numbers.txt"); | |
while (auto opt = istream_helper::Util::next(ifs, to_pair)) | |
{ | |
auto& xs = *opt; | |
std::cout << "sum_all(" << xs.first << ") = " | |
<< std::accumulate(xs.second.begin(), xs.second.end(), 0) << '\n'; | |
} | |
} | |
void example_6() | |
{ | |
std::cout << __FUNCTION__ << '\n'; | |
auto to_ints = [](const std::string& line) { | |
std::istringstream iss{ line }; | |
std::ranges::istream_view<int> isv{ iss }; | |
std::vector<int> temp; | |
std::ranges::copy(isv, std::back_inserter(temp)); | |
return temp; | |
}; | |
std::ifstream ifs1("numbers.txt"), ifs2("numbers.txt"); | |
std::optional<std::string> opt1; std::optional<std::vector<int>> opt2; | |
for (opt1 = istream_helper::Util::next(ifs1), opt2 = istream_helper::Util::next(ifs2, to_ints); | |
opt1 && opt2; | |
opt1 = istream_helper::Util::next(ifs1), opt2 = istream_helper::Util::next(ifs2, to_ints)) | |
{ | |
std::string& s = *opt1; | |
std::vector<int>& xs = *opt2; | |
std::cout << '\t' << s << " - "; | |
for (int x : std::ranges::reverse_view(xs)) | |
std::cout << x << ' '; | |
std::cout << '\n'; | |
} | |
} | |
void example_7() | |
{ | |
std::cout << __FUNCTION__ << '\n'; | |
auto to_ints = [](const std::string& line) { | |
std::istringstream iss{ line }; | |
std::ranges::istream_view<int> isv{ iss }; | |
std::vector<int> temp; | |
std::ranges::copy(isv, std::back_inserter(temp)); | |
return temp; | |
}; | |
std::ifstream ifs("numbers.txt"); | |
std::vector<std::vector<int>> xss; | |
istream_helper::Util::drain(ifs, to_ints, std::back_inserter(xss)); | |
for (const auto& xs : xss) | |
{ | |
std::cout << '\t'; | |
for (int x : xs) std::cout << x << ' '; | |
std::cout << '\n'; | |
} | |
} | |
void example_8() | |
{ | |
std::cout << __FUNCTION__ << '\n'; | |
std::ifstream ifs("numbers.txt"); | |
std::vector<std::string> xs; | |
istream_helper::Util::drain(ifs, std::back_inserter(xs)); | |
for (const auto& s : xs) | |
std::cout << '\t' << s << '\n'; | |
} | |
void example_9() | |
{ | |
std::cout << __FUNCTION__ << '\n'; | |
std::ifstream ifs("numbers.txt"); | |
auto xs = istream_helper::Util::drain(ifs); | |
for (const auto& s : xs) | |
std::cout << '\t' << s << '\n'; | |
} | |
void example_10() | |
{ | |
std::cout << __FUNCTION__ << '\n'; | |
auto xs = istream_helper::Util::drain(std::cin); | |
for (const auto& s : xs) | |
std::cout << '\t' << s << '\n'; | |
} | |
int main() | |
{ | |
void(*fs[])(void) = { | |
example_3, example_4, example_5, example_6, | |
example_7, example_8, example_9, example_10, | |
}; | |
for (auto f : fs) | |
f(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment