C++ Primer(5e)第8章习题

8.1 8.2

#include
#include

using namespace std;

istream& fcn(istream& is)
{
	string word;
	while (is >> word && !is.eof())
	{
		cout << word << endl;
		is.clear();
	}
	return is;
}

int main(int argc, char* argv[])
{
	fcn(cin);
	return 0;
}

8.3

输入类型不合法;文件结束

8.4

#include
#include
#include
#include

using namespace std;

int main(int argc, char* argv[])
{
	string file = "8-4.txt";
	vector<string> str;
	ifstream in(file);
	if (in)
	{
		string word;
		while (getline(in, word))
			str.push_back(word);
	}
	else
		cerr << "couldn't open this file: " << endl;
	for (auto it = str.begin(); it != str.end(); ++it)
		cout << *it << endl;
	return 0;
}

8.5

#include
#include
#include
#include

using namespace std;

int main(int argc, char* argv[])
{
	string file = "8-4.txt";
	vector<string> str;
	ifstream in(file);
	if (in)
	{
		string word;
		while (in >> word)
			str.push_back(word);
	}
	else
		cerr << "couldn't open this file: " << endl;
	for (auto it = str.begin(); it != str.end(); ++it)
		cout << *it << endl;
	return 0;
}

你可能感兴趣的:(C++)