我有这样的代码
std::ifstream file(filename, std::ios_base::in);
if(file.good())
{
file.imbue(std::locale(std::locale(), new delimeter_tokens()));
for(auto& entry : std::istream_iterator<std::string>(file))
{
std::cout << entry << std::endl;
}
}
file.close();
where std::istream_iterator<std::string>
's begin()
and end()
定义如下
template<class T>
std::istream_iterator<T> begin(std::istream_iterator<T>& stream)
{
return stream;
}
template<class T>
std::istream_iterator<T> end(std::istream_iterator<T>& stream)
{
return std::istream_iterator<T>();
}
这是什么马克·纳尔逊多布博士的书中也写到了here。唉,代码无法在我的 Visual Studio 2012 上编译并出现错误消息
错误 C3312:找不到类型“std::istream_iterator<_ty>”的可调用“开始”函数
and
错误 C3312:找不到类型“std::istream_iterator<_ty>”的可调用“end”函数
问题:有什么我没有注意到的,编译器中的错误(不太可能,但以防万一)或者......好吧,有什么想法吗?
根据建议,这个问题已得到很大程度的清理Xeo。为了提供更多背景和参考,这与我的相关其他问题在 Stackoverflow 上,我想知道如何使基于行的解析比通常的循环更干净。从互联网上进行了一些编码和检查,我得到了如下的工作草图
std::ifstream file(filename, std::ios_base::in);
if(file.good())
{
file.imbue(std::locale(std::locale(), new delimeter_tokens()));
for(auto& entry : istream_range<std::string>(file)
{
std::cout << entry << std::endl;
}
}
file.close();
但我试图解决一些小问题。我认为在无法编译的代码中编写看起来更自然,而不是像
for(auto& entry : istream_range<std::string>(file)
请记下不同的迭代器。这delimeter_tokens定义如下Nawaz善意地表明了here(代码不重复)和istream_range如代码合成博客中所示here。我认为开始和结束实现应该有效,正如前面提到的代码综合博客文章中所宣传的那样
The last rule (the fallback to the free-standing begin()and end() functions) allows us to non-invasively adapt an existing container to the range-based for loop interface.
因此,我的问题具有所有相关背景。