完成string从istream流上读取输入

This commit is contained in:
邹晓航
2014-10-12 12:26:43 +08:00
parent 7a66a33901
commit 3f8cc6cbcf

View File

@@ -598,7 +598,7 @@ namespace TinySTL{
return 0;
else if (i == len)
return -1;
else if (j == sublen)
else
return 1;
}
int string::compare(size_t pos, size_t len, const string& str, size_t subpos, size_t sublen) const{
@@ -696,6 +696,32 @@ namespace TinySTL{
}
return os;
}
std::istream& operator >> (std::istream& is, string& str){
char ch;
string::size_type oldSize = str.size(), index = 0;
bool hasPrevBlank = false, badState = false;
while (is.get(ch)){//<2F><><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD>հ<EFBFBD>
if (isblank(ch) || ch == '\n')
hasPrevBlank = true;
else
break;
}
is.putback(ch);
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<61><6D>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
badState = true;
break;
}
}
if (index < oldSize)
str.erase(str.begin() + index, str.end());
return is;
}
string operator+ (const string& lhs, const string& rhs){
string res(lhs);
return res += rhs;