在程序设计中
创建一个vector类型的成员对象
还是采用继承的方式派生出一个类似的对象
一般情况下
面向对象设计更倾向于使用组合而不是继承
//: C07:FileEditor.h
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
// A file editor tool.
#ifndef FILEEDITOR_H
#define FILEEDITOR_H
#include <iostream>
#include <string>
#include <vector>
class FileEditor : public std::vector<std::string> {
public:
void open(const char* filename);
FileEditor(const char* filename) { open(filename); }
FileEditor() {};
void write(std::ostream& out = std::cout);
};
#endif // FILEEDITOR_H ///:~
构造函数打开文件
将其读入到FileEditor
再用成员函数write()将包含string的vector写入任何一个输出流ostream
//: C07:FileEditor.cpp {O}
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
#include "FileEditor.h"
#include <fstream>
#include "../require.h"
using namespace std;
void FileEditor::open(const char* filename) {
ifstream in(filename);
assure(in, filename);
string line;
while(getline(in, line))
push_back(line);
}
// Could also use copy() here:
void FileEditor::write(ostream& out) {
for(iterator w = begin(); w != end(); w++)
out << *w << endl;
} ///:~
来自StringVector.cpp的函数在这里仅被重新打包
程序员在开始时创建一个程序来解决某一特殊的应用
//: C07:FEditTest.cpp
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
//{L} FileEditor
// Test the FileEditor tool.
#include <sstream>
#include "FileEditor.h"
#include "../require.h"
using namespace std;
int main(int argc, char* argv[]) {
FileEditor file;
if(argc > 1) {
file.open(argv[1]);
} else {
file.open("FEditTest.cpp");
}
// Do something to the lines...
int i = 1;
FileEditor::iterator w = file.begin();
while(w != file.end()) {
ostringstream ss;
ss << i++;
*w = ss.str() + ": " + *w;
++w;
}
// Now send them to cout:
file.write();
getchar();
} ///:~
输出 要把 FEditTest.cpp 行目录
1: //: C07:FEditTest.cpp
2: // From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
3: // (c) 1995-2004 MindView, Inc. All Rights Reserved.
4: // See source code use permissions stated in the file 'License.txt',
5: // distributed with the code package available at www.MindView.net.
6: //{L} FileEditor
7: // Test the FileEditor tool.
8: #include <sstream>
9: #include "FileEditor.h"
10: #include "../require.h"
11: using namespace std;
12:
13: int main(int argc, char* argv[]) {
14: FileEditor file;
15: if(argc > 1) {
16: file.open(argv[1]);
17: } else {
18: file.open("FEditTest.cpp");
19: }
20: // Do something to the lines...
21: int i = 1;
22: FileEditor::iterator w = file.begin();
23: while(w != file.end()) {
24: ostringstream ss;
25: ss << i++;
26: *w = ss.str() + ": " + *w;
27: ++w;
28: }
29: // Now send them to cout:
30: file.write();
31: getchar();
32: } ///:~
用于读取文件的操作都在构造函数中
程序块被包含进来用以对内存中的文件进行修改