6
我想用它的範圍構造函數,然後輸出他們通過cout
加載從一個文本文件中的所有行成vector<string
:如何正確使用矢量範圍構造函數?
#include<iostream>
#include<fstream>
#include<vector>
#include<iterator>
using namespace std;
int main()
{
ifstream file("file.txt");
vector<string> strings(istream_iterator<string>(file) , istream_iterator<string>());
for(auto s : strings)
cout << s << endl;
return 0;
}
當試圖編譯上面的代碼,我得到幾個錯誤,例如:
error: no matching function for call to ‘begin(std::vector<std::basic_string<char> > (&) (std::istream_iterator<std::basic_string<char> >, std::istream_iterator<std::basic_string<char> > (*) ()))’
for(auto s : strings)
^
和其他幾個人......
我想我缺少明顯的東西在這裏,任何人都可以請幫助?
請參閱[Most Vexing Parse](http://en.wikipedia.org/wiki/Most_vexing_parse)。 – chris
這可能有所幫助:http://stackoverflow.com/questions/7007817/a-confusing-detail-about-the-most-vexing-parse – templatetypedef
明白了。我應該刪除這個問題還是自己回答? – JLagana