Distance between istream_iterators

∥☆過路亽.° 提交于 2019-11-28 12:45:30

std::istream_iterator is an InputIterator, which means it only supports a single pass over the range you're iterating. There's no way to figure out the size, and then go back to the beginning to read the data.

You can read from std::cin (or any other input stream) using std::copy without knowing the size beforehand, just use std::back_inserter to append the data being read to the vector.

std::vector v;
std::copy( std::istream_iterator<std::string>(std::cin), 
           std::istream_iterator<std::string>(),
           std::back_inserter(v) );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!