From 4f0a4ed75e5aa575f773e720bcb0d56fb2dc11ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Sun, 12 Oct 2014 13:08:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E6=89=80=E6=9C=89string?= =?UTF-8?q?=E7=9A=84=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/String.h | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/TinySTL/String.h b/TinySTL/String.h index e79b782..e598500 100644 --- a/TinySTL/String.h +++ b/TinySTL/String.h @@ -699,7 +699,7 @@ namespace TinySTL{ std::istream& operator >> (std::istream& is, string& str){ char ch; string::size_type oldSize = str.size(), index = 0; - bool hasPrevBlank = false, badState = false; + bool hasPrevBlank = false; while (is.get(ch)){//跳过前导空白 if (isblank(ch) || ch == '\n') hasPrevBlank = true; @@ -707,21 +707,29 @@ namespace TinySTL{ break; } is.putback(ch); + str.clear(); while (is.get(ch)){ if (ch != EOF && !isblank(ch) && ch != '\n'){ - if (++index <= oldSize) - str[index - 1] = ch; - else - str.push_back(ch); - }else{//istream读取出错 - badState = true; + str.push_back(ch); + }else break; - } } - if (index < oldSize) - str.erase(str.begin() + index, str.end()); return is; } + std::istream& getline(std::istream& is, string& str, char delim){ + char ch; + str.clear(); + while (is.get(ch)){ + if (ch == delim) + break; + else + str.push_back(ch); + } + return is; + } + std::istream& getline(std::istream& is, string& str){ + return getline(is, str, '\n'); + } string operator+ (const string& lhs, const string& rhs){ string res(lhs); return res += rhs;