在cpp中将单词从一个文件复制到另一个文件

dik*_*231 8 c++ file

我正在尝试将单词从一个文件复制到另一个文件,这是我的代码:

int main()
{

    string from, to;

    cin >> from >> to;

    ifstream ifs(from);

    ofstream ofs(to);

    set<string> words(istream_iterator<string>(ifs), istream_iterator<string>());
    copy(words.begin(), words.end(), ostream_iterator<string>(ofs, "\n"));

    return !ifs.eof() || !ofs;
}
Run Code Online (Sandbox Code Playgroud)

这样我得到一个编译错误:

expression must have class type
Run Code Online (Sandbox Code Playgroud)

在我称之为copy()的行

如果我将迭代器的结构更改为以下它的工作原理:

set<string> words{ istream_iterator<string>{ ifs }, istream_iterator<string>{} };
Run Code Online (Sandbox Code Playgroud)

我想在cpp中初始化对象时选择()和{}只是一个选择问题,但我想我错了.谁可以给我解释一下这个 ?

kra*_*ich 2

在第一个代码片段中,该set<string> words(istream_iterator<string>(ifs), istream_iterator<string>())行被解析为一个函数的声明words,该函数有两个参数:istream_iterator<string> ifs一个类型的未命名参数istream_iterator<string>,并返回一个set<string>. 这就是它给出编译错误的原因。第二个不能被解析为函数声明,因此它可以正常工作。