@@ -66,7 +66,7 @@ vector<string> read_lines_from_file(string &file_name) {
66
66
string line;
67
67
68
68
ifstream file_handle (file_name.c_str());
69
- while (file_handle.good() && !file_handle.eof()) {
69
+ while (file_handle.good() && !file_handle.eof() && file_handle.peek()!=EOF ) {
70
70
getline(file_handle, line);
71
71
lines.push_back(line);
72
72
}
@@ -89,7 +89,7 @@ int main(int argc, char* argv[]) {
89
89
输出:
90
90
91
91
```cpp
92
- File makefile contains 38 lines.
92
+ File makefile contains 37 lines.
93
93
```
94
94
95
95
这看起来很简单。` vector ` 被填满、返回和调用。然而,作为关心性能的高效程序员,这方面的一些问题困扰着我们:在return语句中,由于使用了值语义,` vector ` 在销毁之前不久就被复制到一个新` vector ` 中。
@@ -104,7 +104,7 @@ vector<string> * read_lines_from_file(string &file_name) {
104
104
string line;
105
105
106
106
ifstream file_handle (file_name.c_str());
107
- while (file_handle.good() && !file_handle.eof()) {
107
+ while (file_handle.good() && !file_handle.eof() && file_handle.peek()!=EOF ) {
108
108
getline(file_handle, line);
109
109
lines->push_back(line);
110
110
}
@@ -147,7 +147,7 @@ vector<string> * read_lines_from_file(string &file_name) {
147
147
string line;
148
148
149
149
ifstream file_handle (file_name.c_str());
150
- while (file_handle.good() && !file_handle.eof()) {
150
+ while (file_handle.good() && !file_handle.eof() && file_handle.peek()!=EOF ) {
151
151
getline(file_handle, line);
152
152
lines->push_back(line);
153
153
}
@@ -265,7 +265,7 @@ unique_ptr<vector<string>> read_lines_from_file(string &file_name) {
265
265
string line;
266
266
267
267
ifstream file_handle (file_name.c_str());
268
- while (file_handle.good() && !file_handle.eof()) {
268
+ while (file_handle.good() && !file_handle.eof() && file_handle.peek()!=EOF ) {
269
269
getline(file_handle, line);
270
270
lines->push_back(line);
271
271
}
@@ -309,4 +309,4 @@ RAII代表“资源获取是初始化”。
309
309
310
310
> 1.https://www.toptal.com/software/eliminating-garbage-collector#remote-developer-job
311
311
312
- > 2.https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii
312
+ > 2.https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii
0 commit comments