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); return 0; }
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; return 0; }
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; return 0; }
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); return 0; }
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; } return 0; }
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; for(int i=0; i<vec.size(); ++i) { isout.clear(); isout.str(vec[i]); string word; while(isout>>word) cout<<word<<endl; } return 0; }
8.12
?
8.13
暂略。。
时间: 2024-10-22 11:35:04