std::find_if
을 사용하여 역순으로 std::streambuf
의 내용을 트래버스합니다. 여기에는 std::istream_iterator
또는 std::istreambuf_iterator
에서 std::reverse_iterator
을 생성하는 과정이 포함됩니다. 불행히도 아래 코드 샘플에서 볼 수 있듯이이 작업을 시도하면 컴파일 오류가 발생합니다. 어떻게 작동시킬 수 있습니까? 필요한 경우 Boost를 사용하는 솔루션이 유용 할 것입니다.역순으로 스트림 반복
#include <cstddef>
#include <fstream>
#include <iterator>
template <class Iterator>
static std::reverse_iterator<Iterator>
make_reverse_iterator(Iterator i)
{
return std::reverse_iterator<Iterator>(i);
}
int main()
{
std::ifstream is{"foo.txt", std::ios::binary};
std::istreambuf_iterator<char> i{is};
auto r = make_reverse_iterator(i);
// Error =(
*r;
return EXIT_SUCCESS;
}
은 컴파일 오류가 g++-4.8.1
에 의해보고 : 당신의 도움에 대한
In file included from /opt/local/include/gcc48/c++/bits/stl_algobase.h:67:0,
from /opt/local/include/gcc48/c++/bits/char_traits.h:39,
from /opt/local/include/gcc48/c++/ios:40,
from /opt/local/include/gcc48/c++/istream:38,
from /opt/local/include/gcc48/c++/fstream:38,
from ri.cpp:9:
/opt/local/include/gcc48/c++/bits/stl_iterator.h: In instantiation of 'std::reverse_iterator<_Iterator>::reference std::reverse_iterator<_Iterator>::operator*() const [with _Iterator = std::istream_iterator<char>; std::reverse_iterator<_Iterator>::reference = const char&]':
ri.cpp:24:3: required from here
/opt/local/include/gcc48/c++/bits/stl_iterator.h:163:10: error: no match for 'operator--' (operand type is 'std::istream_iterator<char>')
return *--__tmp;
^
감사합니다!
컴파일 오류가 무엇인가요? – Borgleader
@Borgleader 컴파일 오류를 포함하도록 질문을 편집했습니다. –
스트림을 역방향으로 반복 하시겠습니까? 모든 스트림이 "되감기"할 수있는 것은 아니기 때문에 매우 일반적인 방법이 있다고 생각합니다. – John3136