我正在閱讀「加速C++」一書的第8章。 8.3節是關於輸入和輸出迭代器:輸入迭代器的值初始化
vector<int> v; // read ints from the standard input and append them to v copy(istream_iterator<int>(cin), istream_iterator<int>(), back_inserter(v));
[...]
The second argument to copy creates a default (empty) istream_iterator, which is not bound to any file. The istream_iterator type has a default value with the property that any istream_iterator that has reached end-of-file or is in an error state will appear to be equal to the default value. Therefore, we can use the default value to indicate the "one-past-the-end" convention for copy.
這是我的理解:istream_iterator是一個模板類,並istream_iterator < int>的是模板的一個實例。寫入istream_iterator < int>()觸發istream_iterator對象的值初始化,這意味着零初始化+調用隱式默認構造函數(http://en.cppreference.com/w/cpp/language/value_initialization)。我認爲istream_iterator < INT的默認初始化>對象將工作,以及(觸發器調用默認的構造函數),所以我想這:
vector<int> v; // read ints from the standard input and append them to v copy(istream_iterator<int>(cin), istream_iterator<int>, back_inserter(v));
但是,這並不編譯:
error: expected primary-expression before ‘,’ token
我不明白髮生了什麼事。歡迎任何解釋。