Primer C++笔记记录(第八章)

本书为Primer C++ 中文第五版

练习题笔记

在这里插入图片描述

(8.1)
std::istream& input(std::istream &is) {
    int n;
    while(is >> n && n != 0) {
        std::cout << n << "\n";
    }
    is.clear();
    return is;
}
(8.2)
int main() {
    input(std::cin);
    return 0;
}
(8.3) 
错误类型 或者 eof()

在这里插入图片描述

(8.4) 
void t84() {
    std::vector<std::string> lines;
    std::ifstream fin;
    fin.open("test.txt");
    std::string line;
    while(!fin.eof()) {
        getline(fin, line);
        lines.push_back(line);
    }
    fin.close();
    lines.pop_back();
    for(auto l : lines) {
        std::cout << l << "\n";
    }
}
(8.5) 
void t85() {
    std::vector<std::string> words;
    std::ifstream fin;
    fin.open("test.txt");
    std::string word;
    while(!fin.eof()) {
        fin >> word;
        words.push_back(word);
    }
    fin.close();
    words.pop_back();
    for(auto l : words) {
        std::cout << l << "\n";
    }
}
(8.6) 
int main(int argc, char *argv[]) {
    if(argc != 2) {
        return -1;
    }
    std::ifstream fin;
    fin.open(argv[1]);
    if(fin) {
        while(!fin.eof()) {
            std::string goods;
            getline(fin, goods);
            std::cout << goods << "\n";
        }
    }
    fin.close();
    return 0;
}

在这里插入图片描述

(8.7)
#include <fstream>
#include <string>

int main(int argc, char *argv[]) {
    if(argc != 3) {
        return -1;
    }
    std::ifstream fin;
    std::ofstream fout;
    fout.open(argv[2]);
    fin.open(argv[1]);
    std::string item;
    if(fin && fout) {
        while(!fin.eof() && fin.peek() != EOF) {
            getline(fin, item);
            fout << item << "\n";
        }
    }
    fin.close();
	fout.close();
    return 0;
}
(8.8)
#include <fstream>
#include <string>

int main(int argc, char *argv[]) {
    if(argc != 3) {
        return -1;
    }
    std::ifstream fin;
    std::ofstream fout;
    fout.open(argv[2], std::ofstream::app);
    fin.open(argv[1]);
    std::string item;
    if(fin && fout) {
        while(!fin.eof() && fin.peek() != EOF) {
            getline(fin, item);
            fout << item << "\n";
        }
    }
    fin.close();
	fout.close();
    return 0;
}

在这里插入图片描述

(8.9)
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>

void t89() {
    std::vector<std::string> words;
    std::ifstream fin;
    fin.open("test.txt");
    std::string word;
    while(!fin.eof()) {
		std::string line;
        getline(fin, line);
		std::istringstream record(line);
		while(record >> word)
        	words.push_back(word);
    }
    fin.close();
    for(auto w : words) {
        std::cout << w << "\n";
    }
}

int main() {
    t89();
    return 0;
}
(8.10)
见8.9
(8.11)
void t811() {
    std::vector<std::string> words;
    std::ifstream fin;
    fin.open("test.txt");
    std::string word;
	std::istringstream record;
    while(!fin.eof()) {
		std::string line;
        getline(fin, line);
		record.clear();//清除缓存
		record.str(line);
		while(record >> word)
        	words.push_back(word);
    }
    fin.close();
    for(auto w : words) {
        std::cout << w << "\n";
    }
}

int main() {
    t811();
    return 0;
}
(8.12)
因为每个人的电话号码的数量不一致

在这里插入图片描述

(8.13)
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>

using namespace std;

struct PersonInfo {
	string name;
	vector<string> phones;
};

int main() {
	vector<PersonInfo> people;
	string line, word;
	ifstream fin;
	fin.open("test.txt");
	while(!fin.eof() && fin.peek() != EOF) {
		PersonInfo info;
		getline(fin, line);
		istringstream record(line);
		record >> info.name;
		while(record >> word)
			info.phones.push_back(word);
		people.push_back(info);	
	}
	for(auto p : people) {
		cout << p.name << endl;	
	}
	return 0;
}
(8.14)
无需改变类中的变量,最好定义为常量引用,可以避免对象拷贝。

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦