标签:EDA 需要 分享 none const ali bsp 进入 ecb
练习8.1:考察如何管理流的状态
1 istream& func(istream& is) 2 { 3 string buf; 4 while ( is>>buf ) cout<<buf<<endl; 5 is.clear(); 6 return is; 7 }
练习8.2:考察同上
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 5 istream& func(istream& is) 6 { 7 string buf; 8 while ( is>>buf ) cout<<buf<<endl; 9 is.clear(); 10 return is; 11 } 12 13 int main() 14 { 15 istream& is=func(cin); 16 cout<<is.rdstate()<<endl; 17 return 0; 18 }
练习8.3:考察cin的错误状态
输入使得cin进入了错误状态会导致循环终止。如:badbit,failbit,eofbit
练习8.4:考察文件流的使用
1 #include<iostream> 2 #include<fstream> 3 #include<vector> 4 #include<string> 5 using namespace std; 6 7 void ReadFileToVec(const string& fileName,vector<string>& vec) 8 { 9 ifstream ifs(fileName); 10 if ( ifs ) 11 { 12 string buf; 13 while ( getline(ifs,buf) ) vec.push_back(buf); 14 } 15 } 16 17 int main() 18 { 19 vector<string>vec; 20 ReadFileToVec("../date/book.txt",vec); 21 for ( const auto& i:vec ) cout<<i<<endl; 22 return 0; 23 }
练习8.5:考察同上
1 #include<iostream> 2 #include<fstream> 3 #include<vector> 4 #include<string> 5 using namespace std; 6 7 void ReadFileToVec(const string& fileName,vector<string>& vec) 8 { 9 ifstream ifs(fileName); 10 if ( ifs ) 11 { 12 string buf; 13 while ( ifs>>buf ) vec.push_back(buf); 14 } 15 } 16 17 int main() 18 { 19 vector<string>vec; 20 ReadFileToVec("../date/book.txt",vec); 21 for ( const auto& i:vec ) cout<<i<<endl; 22 return 0; 23 }
练习8.9:考察istringstream的使用
1 #include<iostream> 2 #include<string> 3 #include<sstream> 4 using namespace std; 5 6 istream& func(istream& is) 7 { 8 string buf; 9 while ( is>>buf ) cout<<buf<<endl; 10 is.clear(); 11 return is; 12 } 13 14 int main() 15 { 16 istringstream iss("hello world"); 17 func(iss); 18 return 0; 19 }
练习8.10:考察同上
1 #include<iostream> 2 #include<vector> 3 #include<string> 4 #include<fstream> 5 #include<sstream> 6 using namespace std; 7 8 void ReadFileToVec(const string& fileName,vector<string>& vec) 9 { 10 ifstream ifs(fileName); 11 if ( ifs ) 12 { 13 string line; 14 while ( getline(ifs,line) ) vec.push_back(line); 15 } 16 } 17 18 int main() 19 { 20 vector<string>vec; 21 ReadFileToVec("../date/book.txt",vec); 22 for ( auto& s:vec ) 23 { 24 istringstream iss(s); 25 string word; 26 while ( iss>>word ) cout<<word<<endl; 27 } 28 return 0; 29 }
练习8.11:考察同上
在每次使用时需要先清楚原有内容,在重新拷贝
1 #include<iostream> 2 #include<sstream> 3 #include<string> 4 #include<vector> 5 using namespace std; 6 struct PersonInfo{ 7 string name; 8 vector<string>phones; 9 }; 10 11 int main() 12 { 13 string line,word; 14 vector<PersonInfo>people; 15 istringstream record; 16 while ( getline(cin,line) ) 17 { 18 PersonInfo info; 19 record.clear(); 20 record.str(line); 21 record>>info.name; 22 while ( record>>word ) info.phones.push_back(word); 23 people.push_back(info); 24 } 25 }
练习8.12:
参考网上答案:因为这里我们需要聚合类,所以我们不需要使用类内初始化
练习8.13:三种流的综合应用
1 #include<iostream> 2 #include<sstream> 3 #include<string> 4 #include<vector> 5 #include<fstream> 6 using namespace std; 7 struct PersonInfo{ 8 string name; 9 vector<string>phones; 10 }; 11 12 bool valid(const string& s) 13 { 14 return isdigit(s[0]); 15 } 16 17 string format(const string& str) 18 { 19 return str.substr(0,3)+"-"+str.substr(3,3)+"-"+str.substr(6); 20 } 21 22 int main() 23 { 24 ifstream ifs("../date/phonenumbers.txt"); 25 if ( !ifs ) 26 { 27 cerr<<"no phone numbers?"<<endl; 28 return -1; 29 } 30 string line,word; 31 vector<PersonInfo>people; 32 istringstream record; 33 while ( getline(ifs,line) ) 34 { 35 PersonInfo info; 36 record.clear(); 37 record.str(line); 38 record>>info.name; 39 while ( record>>word ) info.phones.push_back(word); 40 people.push_back(info); 41 } 42 for ( const auto& entry:people ) 43 { 44 ostringstream formatted,badNums; 45 for ( const auto& nums:entry.phones ) 46 { 47 if ( !valid(nums) ) badNums<<" "<<nums; 48 else formatted<<" "<<format(nums); 49 } 50 if ( badNums.str().empty() ) 51 { 52 cout<<entry.name<<" "<<formatted.str()<<endl; 53 } 54 else 55 { 56 cerr<<"input error: "<<entry.name<<"invalid number(s)"<<badNums.str()<<endl; 57 } 58 } 59 }
练习8.14:
因为它们都是类不是内置类型,所以使用引用会更加高效
又因为它们在整个过程中并不需要修改,所以使用const
标签:EDA 需要 分享 none const ali bsp 进入 ecb
原文地址:https://www.cnblogs.com/HDUjackyan/p/9569192.html