From eebcddb4729678568e27638041846a5077e9513c Mon Sep 17 00:00:00 2001 From: Manish Date: Fri, 24 Feb 2023 18:21:14 +1100 Subject: [PATCH] fixes: 1. do not skip over first non-empty line; 2. skip over all empty lines --- 1_longest_substring_with_k_distinct_characters.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/1_longest_substring_with_k_distinct_characters.cpp b/1_longest_substring_with_k_distinct_characters.cpp index 6aef394..64b36cb 100644 --- a/1_longest_substring_with_k_distinct_characters.cpp +++ b/1_longest_substring_with_k_distinct_characters.cpp @@ -24,6 +24,7 @@ struct semicolon_is_space : std::ctype { return &rc[0]; }; }; + class input { public: std::string s; @@ -33,9 +34,7 @@ public: }; std::istream &operator>>(std::istream &in, input &i) { - std::getline(in, i.s, '\n'); // An inelegant hack to skip newline character - std::getline(in, i.s, ';'); - in >> i.k; + in >> i.s >> i.k; return in; } @@ -128,10 +127,10 @@ int main(int argc, char *argv[]) { std::vector test_cases; semicolon_is_space delimeter; for (int i = 1; i < argc; i++) { - test_case t; std::ifstream f(argv[i]); f.imbue(std::locale(f.getloc(), new semicolon_is_space)); while (f.good()) { + test_case t; f >> t; if (t.i.s.length()) { // skip empty line/string inputs std::cout << t;