终于到第二部分,给爷冲,干就完了!
8.1
std::istream& iiofunction(std::istream& is)
{
std::string s;
while (is>>s)
{
std::cout << s << std::endl;
}
is.clear();
return is;
}
8.2
#include<iostream>
#include<string>
std::istream& iofunction(std::istream& is)
{
std::string s;
while (is>>s)
{
std::cout << s << std::endl;
}
is.clear();
return is;
}
int main()
{
iofunction(std::cin);
std::string s1;
while (std::cin>>s1)
{
std::cout << s1 << std::endl;
}
return 0;
}
8.3
如果badbit、failbit、eofbit任何一个被置位,则检测流状态的条件会失败;
8.4
“using namespace std” is YYDS!
#include<iostream>
#include<string>
#include<vector>
#include<fstream>
using namespace std;
int main()
{
ifstream ifs("data");
vector<string> v1;
string buf;
if (ifs)
{
while (getline(ifs,buf))
{
v1.push_back(buf);
}
}
for (auto& s : v1)
cout << s << " ";
cout << endl;
return 0;
}
8.5
#include<iostream>
#include<string>
#include<vector>
#include<fstream>
using namespace std;
int main()
{
ifstream ifs("data");
vector<string> v1;
string buf;
if (ifs)
{
while (getline(ifs,buf))
{
v1.push_back(buf);
}
}
for ( const auto& s : v1)
cout << s << " ";
cout << endl;
return 0;
}
8.6
#include <iostream>
#include <string>
#include <fstream>
#include "../ch07_Classes/Sales_data_ex26.h"
int main(int argc, char **argv)
{
std::ifstream ifs(argv[1]);
if(!ifs) return 1;
Sales_data total(ifs);
if (!total.isbn().empty())
{
Sales_data trans;
while (read(ifs, trans))
{
if (total.isbn() == trans.isbn())
{
total.combine(trans);
}
else
{
print(std::cout, total);
std::cout << std::endl;
total = trans;
}
}
print(std::cout, total);
std::cout << std::endl;
return 0;
}
else
{
std::cerr << "No data?!" << std::endl;
return -1; // indicate failure
}
}
8.7
#include <iostream>
#include <string>
#include <fstream>
#include "../ch07_Classes/Sales_data_ex26.h"
int main(int argc, char **argv)
{
std::ifstream ifs(argv[1]);
std::ofstream ofs(argv[2]);
if(!ifs) return 1;
Sales_data total(ifs);
if (!total.isbn().empty())
{
Sales_data trans;
while (read(ifs, trans))
{
if (total.isbn() == trans.isbn())
{
total.combine(trans);
}
else
{
print(ofs, total);
ofs << std::endl;
total = trans;
}
}
print(ofs, total);
ofs << std::endl;
return 0;
}
else
{
std::cerr << "No data?!" << std::endl;
return -1; // indicate failure
}
}
8.8
#include <iostream>
#include <string>
#include <fstream>
#include "../ch07_Classes/Sales_data_ex26.h"
int main(int argc, char **argv)
{
std::ifstream ifs(argv[1]);
std::ofstream ofs(argv[2], std::ofstream::app);
if(!ifs) return 1;
Sales_data total(ifs);
if (!total.isbn().empty())
{
Sales_data trans;
while (read(ifs, trans))
{
if (total.isbn() == trans.isbn())
{
total.combine(trans);
}
else
{
print(ofs, total);
ofs << std::endl;
total = trans;
}
}
print(ofs, total);
ofs << std::endl;
return 0;
}
else
{
std::cerr << "No data?!" << std::endl;
return -1; // indicate failure
}
}
8.9
#include<iostream>
#include<string>
#include<sstream>
std::istream& iofunction(std::istream& is)
{
std::string s;
while (is >> s)
{
std::cout << s << std::endl;
}
is.clear();
return is;
}
int main()
{
std::istringstream istrs("aa\nbb\ncc\ndd");
iofunction(istrs);
std::string s1;
while (std::cin >> s1)
{
std::cout << s1 << std::endl;
}
return 0;
}
8.10
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
using namespace std;
int main()
{
vector<string>v1;
ifstream is("book_sales");
string buf;
if (!is)
{
cerr << "open error" << endl;
return -1;
}
while (getline(is,buf))
{
v1.push_back(buf);
}
for (const auto& s : v1)
{
istringstream iss(s);
while (iss >> buf)
cout << buf << endl;
}
return 0;
}
8.11
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
using namespace std;
struct PersonInfo
{
string name;
vector<string> phones;
};
int main()
{
string line, word;
vector<PersonInfo>people;
istringstream record;
while (getline(cin,line))
{
record.str(line);
PersonInfo info;
record >> info.name;
while (record>>word)
{
info.phones.push_back(word);
}
record.clear();
people.push_back(info);
for (const auto& person : people)
{
cout << person.name << endl;
for (const auto& ph : person.phones)
cout << ph << " ";
cout << endl;
}
}
return 0;
}
8.12
string和vector可以自己初始化,并不会出现未定义的情况;
8.13
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <fstream>
using namespace std;
struct PersonInfo {
string name;
vector<string> phones;
};
bool valid(const string& s)
{
for (const auto c : s)
if (!isdigit(c)) return false;
return true;
}
string format(const string& s)
{
return s;
}
int main()
{
string line, word;
vector<PersonInfo> people;
istringstream record;
ifstream ifs("personinfo");
ofstream ofs("personinfo_new");
while (getline(ifs, line))
{
record.str(line);
PersonInfo info;
record >> info.name;
while (record >> word)
info.phones.push_back(word);
record.clear();
people.push_back(info);
}
for (const auto& person : people)
{
ostringstream formatted, badNums;
for (const auto& ph : person.phones)
{
if (!valid(ph))
{
badNums << " " << ph;
}
else
formatted << " " << format(ph);
}
if (badNums.str().empty())
ofs << person.name << " " << formatted.str() << endl;
else
cerr << " input error: " << person.name << " invalid number(s)" << badNums.str() << endl;
}
return 0;
}
8.14
它们都是类类型,因此使用引用可以避免拷贝,提高效率;
在循环语句中不改变它们的值,因此用const;