8.1
std::istream &print(std::istream &is)
{
char ch;
auto old_state = is.rdstate();
while (is>>ch)
std::cout << ch;
is.setstate(old_state);
return is;
}
8.2
#include <iostream>
std::istream &print(std::istream &is);
int main()
{
using namespace std;
print(cin);
system("pause");
return 0;
}
std::istream &print(std::istream &is)
{
char ch;
auto old_state = is.rdstate();
while (is>>ch)
std::cout << ch;
is.setstate(old_state);
return is;
}
8.3
cin输入流处于错误状态:badbit、failbit、eofbit
例如类型与i不匹配、遇到文件结尾等
8.4
#include <fstream>
#include <string>
#include <iostream>
#include <vector>
int main(int argc, char *argv[])
{
using namespace std;
ifstream ifile(argv[1]);
if (!ifile)
{
cout << "open file error";
exit(1);
}
vector<string> str;
string temp;
int i = 0;
while (getline(ifile, temp))
str.push_back(temp);
for (auto &t : str)
cout << t << endl;
return 0;
}
8.5
#include <fstream>
#include <string>
#include <iostream>
#include <vector>
int main(int argc, char *argv[])
{
using namespace std;
ifstream ifile(argv[1]);
if (!ifile)
{
cout << "open file error";
exit(1);
}
vector<string> str;
string temp;
int i = 0;
while (ifile>>temp)
str.push_back(temp);
for (auto &t : str)
cout << t << endl;
return 0;
}
8.6
#include <iostream>
#include <fstream>
#include <string>
struct Sales_data{
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
int main(int argc, char *argv[])
{
using namespace std;
ifstream ifile(argv[1]);
if (!ifile)
{
cout << "open file error";
exit(1);
}
Sales_data total;
if (ifile >> total.bookNo >> total.units_sold >> total.revenue)
{
Sales_data trans;
while (ifile >> trans.bookNo >> trans.units_sold >> trans.revenue) {
if (total.bookNo == trans.bookNo) {
total.units_sold += trans.units_sold;
total.revenue += trans.revenue;
}
else {
cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;
total = trans;
}
}
cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;
}
else
{
std::cerr << "No data?!" << std::endl;
return -1;
}
return 0;
}
8.7
#include <iostream>
#include <fstream>
#include <string>
struct Sales_data{
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
int main(int argc, char *argv[])
{
using namespace std;
ifstream ifile(argv[1]);
if (!ifile)
{
cout << "open file error";
exit(1);
}
ofstream ofile(argv[2]);
if (!ofile)
{
cout << "open file error";
exit(1);
}
Sales_data total;
if (ifile >> total.bookNo >> total.units_sold >> total.revenue)
{
Sales_data trans;
while (ifile >> trans.bookNo >> trans.units_sold >> trans.revenue) {
if (total.bookNo == trans.bookNo) {
total.units_sold += trans.units_sold;
total.revenue += trans.revenue;
}
else {
ofile << total.bookNo << "\t" << total.units_sold << "\t" << total.revenue << endl;
total = trans;
}
}
ofile << total.bookNo << "\t" << total.units_sold << "\t" << total.revenue << endl;
}
else
{
std::cerr << "No data?!" << std::endl;
return -1;
}
return 0;
}
8.8
#include <iostream>
#include <fstream>
#include <string>
struct Sales_data{
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
int main(int argc, char *argv[])
{
using namespace std;
ifstream ifile(argv[1]);
if (!ifile)
{
cout << "open file error";
exit(1);
}
ofstream ofile(argv[2], ios_base::app); //追加
if (!ofile)
{
cout << "open file error";
exit(1);
}
Sales_data total;
if (ifile >> total.bookNo >> total.units_sold >> total.revenue)
{
Sales_data trans;
while (ifile >> trans.bookNo >> trans.units_sold >> trans.revenue) {
if (total.bookNo == trans.bookNo) {
total.units_sold += trans.units_sold;
total.revenue += trans.revenue;
}
else {
ofile << total.bookNo << "\t" << total.units_sold << "\t" << total.revenue << endl;
total = trans;
}
}
ofile << total.bookNo << "\t" << total.units_sold << "\t" << total.revenue << endl;
}
else
{
std::cerr << "No data?!" << std::endl;
return -1;
}
return 0;
}
8.9
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
istream &func(istream &is)
{
auto olds = is.rdstate();
string str;
while (is >> str)
{
cout << str;
}
is.setstate(olds);
return is;
}
int main()
{
string s("c++ c c++p c++pp windows");
istringstream ssfile(s);
func(ssfile);
system("pause");
return 0;
}
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
int main(int argc, char **argv)
{
using namespace std;
ifstream ifile(argv[1]);
if (!ifile)
{
cerr << "open file error!\n";
exit(1);
}
vector<string> str;
string st;
while (getline(ifile, st))
{
istringstream iss(st);
while (iss >> st)
{
cout << st << ends;
}
}
return 0;
}
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
struct PersonInfo
{
std::string name;
std::vector<std::string> phones;
};
int main()
{
using namespace std;
istringstream record;
string line, word;
vector<PersonInfo> people;
while (getline(cin, line))
{
PersonInfo info;
record.str(line);
record >> info.name;
while (record >> word)
info.phones.push_back(word);
people.push_back(info);
record.clear();
}
for (auto &x : people)
{
cout << x.name << "\t";
for (auto a : x.phones)
cout << a << "\t";
cout << endl;
}
system("pause");
return 0;
}
没有意义,这个类作为结构体使用,而且我们需要的是一个空的对象,会给他所有成员赋值,不存在成员空值
8.13
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct PersonInfo {
string name;
vector<string> phones;
};
bool valid(const string& str)
{
return isdigit(str[0]);
}
string format(const string& str)
{
return str.substr(0, 3) + "-" + str.substr(3, 3) + "-" + str.substr(6);
}
int main(int argc,char **argv)
{
ifstream ifs(argv[1]);
if (!ifs)
{
cerr << "open file error!" << endl;
return -1;
}
string line, word;
vector<PersonInfo> people;
istringstream record;
while (getline(ifs, 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)
{
ostringstream formatted, badNums;
for (const auto &nums : entry.phones)
{
if (!valid(nums))
badNums << " " << nums;
else
formatted << " " << format(nums);
if (badNums.str().empty())
cout << entry.name << " " << formatted.str() << endl;
else
cerr << "input error: " << entry.name << " invalid number(s) " << badNums.str() << endl;
}
}
return 0;
}
9.14
不需要改变数据和反正改变数据
引用会跟更快,因为不需要调用复制构造函数
so~~~~睡觉
2015年11月11日21:46:27 希望明年能够脱单