8.1、8.2
这一章不咋会啊。。
istream &read(istream &is) { int a; auto old_state=is.rdstate(); is.clear(); is>>a; is.setstate(old_state); return is; } int main() { read(cin); ; }
8.3
读到eof或错误类型的时候
8.4
#include<fstream> using namespace std; int main() { ifstream in("ifile.txt"); vector<int> vec; int a; while(in>>a) vec.push_back(a); for(auto i:vec) cout<<i<<endl; ; }
8.5
#include<fstream> using namespace std; int main() { ifstream in("ifile.txt"); vector<string> vec; string s; while(in>>s) vec.push_back(s); for(auto i:vec) cout<<i<<endl; ; }
8.6、8.7、8.8
暂略。。
8.9
#include<sstream> using namespace std; istream &read(istream &is) { string s; auto old_state=is.rdstate(); is.clear(); while(is>>s) cout<<s<<endl; is.setstate(old_state); return is; } int main() { string s; getline(cin,s); istringstream is(s); read(is); ; }
8.10
#include<fstream> #include<sstream> using namespace std; int main() { fstream in("ifile.txt"); string s; vector<string> vec; while(getline(in,s)) { vec.push_back(s); } for(auto &c:vec) { istringstream isout(c); string word; while(isout>>word) cout<<word<<endl; } ; }
8.11
使用clear和str两个成员函数
#include<fstream> #include<sstream> using namespace std; int main() { fstream in("ifile.txt"); string s; vector<string> vec; while(getline(in,s)) { vec.push_back(s); } istringstream isout; ; i<vec.size(); ++i) { isout.clear(); isout.str(vec[i]); string word; while(isout>>word) cout<<word<<endl; } ; }
8.12
?
8.13
暂略。。