📔 C++ Primer 0x08 练习题解
更好的阅读体验
推荐阅读 《C++ Primer 5th》知识点总结&练习题解
8.1 IO类
8.1.2 条件状态
#include <iostream>
std::istream& fun(std::istream& is){
std::string buf;
while(is >> buf){
std::cout << buf << std::endl;
}
is.clear();
return is;
}
int main(){
std::istream& is = fun(std::cin);
std::cout << is.rdstate() << std::endl;
return 0;
}
while (cin >> i) /* ... */
- 如果
badbit
、failbit
和eofbit
任一被置位,检测流状态的条件就会失败
8.2 文件输入输出
void read(const std::string& ifile,std::vector<std::string>& vec){
std::ifstream input(ifile);
if(input){
std::string buf;
while(geline(input,buf)){
vec.push_back(buf);
}
}
}
void read(const std::string& ifile,std::vector<std::string>& vec){
std::ifstream input(ifile);
if(input){
std::string buf;
while(input >> buf){
vec.push_back(buf);
}
}
}
#include <iostream>
#include <string>
#include <fstream>
struct Sales_data {
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
int main(int argc,char **argv){
std::ifstream input(argv[1]);
Sales_data total,item;
double price = 0;
if(input){
if (input >> total.bookNo >> total.units_sold >> price){
total.revenue = total.units_sold * price;
while (input >> item.bookNo >> item.units_sold >> price){
item.revenue = item.units_sold * price;
if (total.bookNo == item.bookNo) {
total.units_sold += item.units_sold;
total.revenue += item.revenue;
}else {
std::cout << total.bookNo << " "
<< total.units_sold << " "
<< total.revenue << std::endl;
total = item;
}
}
std::cout << total.bookNo << " "
<< total.units_sold << " "
<< total.revenue << std::endl;
}else {
std::cerr << "No data?!" << std::endl;
return -1;
}
}
return 0;
}
8.2.2 文件模式
#include <iostream>
#include <string>
#include <fstream>
struct Sales_data {
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
int main(int argc,char **argv){
std::ifstream input(argv[1]);
std::ofstream output(argv[2]);
Sales_data total,item;
double price = 0;
if(input){
if (input >> total.bookNo >> total.units_sold >> price){
total.revenue = total.units_sold * price;
while (input >> item.bookNo >> item.units_sold >> price){
item.revenue = item.units_sold * price;
if (total.bookNo == item.bookNo) {
total.units_sold += item.units_sold;
total.revenue += item.revenue;
}else {
output << total.bookNo << " "
<< total.units_sold << " "
<< total.revenue << std::endl;
total = item;
}
}
output << total.bookNo << " "
<< total.units_sold << " "
<< total.revenue << std::endl;
}else {
std::cerr << "No data?!" << std::endl;
return -1;
}
}
return 0;
}
#include <iostream>
#include <istream>
#include <string>
#include <fstream>
struct Sales_data {
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
int main(int argc,char **argv){
std::ifstream input(argv[1]);
std::ofstream output(argv[2],std::ofstream::app);
Sales_data total,item;
double price = 0;
if(input){
if (input >> total.bookNo >> total.units_sold >> price){
total.revenue = total.units_sold * price;
while (input >> item.bookNo >> item.units_sold >> price){
item.revenue = item.units_sold * price;
if (total.bookNo == item.bookNo) {
total.units_sold += item.units_sold;
total.revenue += item.revenue;
}else {
output << total.bookNo << " "
<< total.units_sold << " "
<< total.revenue << std::endl;
total = item;
}
}
output << total.bookNo << " "
<< total.units_sold << " "
<< total.revenue << std::endl;
}else {
std::cerr << "No data?!" << std::endl;
return -1;
}
}
return 0;
}
8.8 string 流
8.3.1 使用 istringstream
#include <iostream>
#include <sstream>
std::istream& fun(std::istream& is){
std::string buf;
while(is >> buf){
std::cout << buf << std::endl;
}
is.clear();
return is;
}
int main(){
std::string line;
getline(std::cin,line);
std::istringstream record(line);
std::istream& is = fun(record);
std::cout << is.rdstate() << std::endl;
return 0;
}
#include <iostream>
#include <sstream>
#include <vector>
int main(){
std::string line,word;
std::vector<std::string>vec;
while(getline(std::cin,line)){
std::istringstream record(line);
if(record){
while(record >> word){
vec.push_back(word);
}
}
}
for(auto v:vec)std::cout << v << std::endl;
return 0;
}
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
struct PersonInfo {
std::string name;
std::vector<std::string> phones;
};
int main(){
std::string line, word;
std::vector<PersonInfo> people;
std::istringstream record;
while (getline(std::cin, line)){
PersonInfo info;
record.clear();record.str(line);
record >> info.name;
while (record >> word)
info.phones.push_back(word);
people.push_back(info);
}
for (auto &p : people){
std::cout << p.name << " ";
for (auto &s : p.phones)
std::cout << s << " ";
std::cout << std::endl;
}
return 0;
}
只需要聚合类即可,不需要类内初始化
8.3.2 使用 ostringstream
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <fstream>
struct PersonInfo {
std::string name;
std::vector<std::string> phones;
};
bool vaild(const std::string& str){
return isdigit(str[0]);
}
std::string format(const std::string& str){
return str.substr(0,3) + "-" + str.substr(3,3) + "-" + str.substr(6);
}
int main(int argc,char **argv){
std::ifstream input(argv[1]);
std::string line, word;
std::vector<PersonInfo> people;
std::istringstream record;
while (getline(input, line)){
PersonInfo info;
record.clear();record.str(line);
record >> info.name;
while (record >> word)
info.phones.push_back(word);
people.push_back(info);
}
for(const auto &entry:people){
std::ostringstream formatted,badNums;
for(const auto &nums:entry.phones){
if(!vaild(nums)){
badNums << " " << nums;
}else{
formatted << " " << nums;
}
}
if(badNums.str().empty())
std::cout << entry.name << " " << formatted.str() << std::endl;
else
std::cerr << "input error: " << entry.name
<< "invaild number(s) " << badNums.str() << std::endl;
}
return 0;
}
不需要改变,所以用const
它们是类类型,使用引用可以避免拷贝: