我该如何去使用构造函数来处理一个字符串的成员?下面是一个例子(这是不对的我知道)C++字符串成员构造
class Filestring {
public:
string sFile;
Filestring(const string &path)
{
ifstream filestream(path.c_str());
// How can I use the constructor for the member sFile??
// I know this is wrong, but this illustrates what I want to do.
string sFile((istreambuf_iterator<char>(filestream)), istreambuf_iterator<char>());
}
};
所以基本上我希望能够使用成员sFile的构造而不做字符串拷贝。有没有办法通过分配来实现这一点?
你为什么这么关心一个字符串复制?如果您正在从文件中读取数据,则文件I/O时间将占用大部分运行时间。 – 2012-04-18 19:58:46
只是为了清楚这一点?你是否试图用文件的内容初始化sFile? – smichak 2012-04-18 20:01:03
@smichak正确 – grep 2012-04-18 20:07:58