我在下面编写了应该保存 istream 迭代器的代码。我想要的是保留迭代器以便以后复制到给定位置的字符串。这工作正常,直到我将流重置为开头。输出是“teet”之二我想要“tees”。有没有办法使迭代器独立于流位置?
#include <iostream>
#include <sstream>
#include <iterator>
int main(){
std::stringstream test;
test << "test 123456\n";
std::istream_iterator<char> it(test);
std::cout << *it;
it++;
std::cout << *it;
test.clear();
test.seekg(0, std::ios::beg);
std::cout << *it;
it++;
std::cout << *it;
}