WARC 파일을 구문 분석하기 위해 C++ 라이브러리를 작성 중이며 WARC 레코드 및 필드 데이터를 저장하는 클래스와 일부 입력 스트림에서 데이터를 읽는 연산자를 포함합니다. istream
에서 읽고 WARCRecord
개체를 채우려면 operator>>
이 있어야합니다. istream
스트림에서 읽을 수 있고 WARCRecord
개체로 구문 분석 할 수 있습니다. 그러나 입력 스트림에서 모든 WARC 레코드를 읽으려면 istream_iterator
을 사용하고 싶습니다.istream_iterator 객체를 다시 사용합니까?
istream_iterator
을 사용하여 스트림의 끝까지 반복 할 때 내 operator>>
은 마지막 호출의 동일한 (copy?) 객체를받습니다. 이것은 예상되는 동작입니까, 아니면 클래스 정의에 연산자/생성자가 없습니다.
#include <iostream>
#include <istream>
#include <sstream>
#include <vector>
#include <iterator>
class MyClass
{
public:
std::vector<unsigned int> fields;
friend std::istream& operator>> (std::istream& is, MyClass& obj);
};
std::istream& operator>> (std::istream& is, MyClass& obj){
std::istream::sentry s(is);
if (s) {
for (unsigned i = 0; i < 3; i++) {
unsigned f;
is >> f;
obj.fields.push_back(f);
}
}
return is;
}
int main(void)
{
std::stringstream ss;
ss << 1 << " " << 2 << " " << 3 << " " << 4 << " " << 5 << " " << 6;
std::istream_iterator<MyClass> it(ss);
std::cout << it->fields.size() << std::endl;
it++;
std::cout << it->fields.size() << std::endl;
return 0;
}
이 프로그램의 출력은 :
$ ./a.out
3
6
https://github.com/jvanz/libwarc
간단한 예는 상기 텍스트를 발휘 :
이이 동작을 확인하는 단위 테스트와 저장소 링크는
MyClass
개체의 fields
개체 크기가 누적됩니다 (
그것은'표준 : istream_iterator'의 반복자가 T''의 새로운 객체마다 반복자는 증가를 생성할지 여부를 지정합니다. 따라서 구현은 매번 동일한 내부 객체를 다시 사용할 수 있으며, 참조가 해제되면 다시 반환 할 수 있습니다. –
WhiZTiM
코드를 연결하는 대신 여기에 붙여 넣습니다. 그것이 [mcve] –