istream-iterator

Distance between istream_iterators

自闭症网瘾萝莉.ら 提交于 2019-12-17 20:38:43
问题 I am trying to count the amount of elements read by std::cin by using std::distance for the constructor argument of a vector in advance, like so: // Gives correct amount, but now can't use to put into vector int size = std::distance(std::istream_iterator<std::string>(std::cin), std::istream_iterator<std::string>()); std::vector v(size); // Already read from stream std::copy(...); Obviously I can complete this in one step with std::copy , but that would require setting a size beforehand. My

std::istream_iterator<> with copy_n() and friends

帅比萌擦擦* 提交于 2019-12-17 16:10:15
问题 The snippet below reads three integers from std::cin ; it writes two into numbers and discards the third: std::vector<int> numbers(2); copy_n(std::istream_iterator<int>(std::cin), 2, numbers.begin()); I'd expect the code to read exactly two integers from std::cin , but it turns out this is a correct, standard-conforming behaviour. Is this an oversight in the standard? What is the rationale for this behaviour? From 24.5.1/1 in the C++03 standard: After it is constructed, and every time ++ is

Can one make move_iterator from istream_iterator?

。_饼干妹妹 提交于 2019-12-13 13:31:21
问题 Consider following code: typedef istream_iterator<char> char_itr ; char_itr eos; string ll("some text here"); istringstream line_in(ll); char_itr start(line_in); move_iterator<char_itr> mstart(start); // !!! move_iterator<char_itr> meos(eos); vector<char> vc(mstart, meos); Above code will not compile because of line (!!!): error C2440: 'return' : cannot convert from 'const char' to 'char &&' But if you replace mstart and meos with start and eos , respectively (regular iterators), the code

boost spirit istream iterator giving false positives

耗尽温柔 提交于 2019-12-12 20:19:09
问题 So I'm trying to get spirit to parse the characters from this file as it's input. I'd rather not read the full string into memory if at all possible. This is my current relevant code, the Rosters_Grammar is a grammar file that I am using to specify my desired grammar. #include "StdAfx.h" #include "Interpreter.h" #include "Rosters_Grammar.h" #include <boost\spirit\include\qi.hpp> #include <fstream> bool Interpreter::invoke(std::string path) { //Define our parser type and iterator types.

Strange Error in using template<class InputIterator> string (InputIterator begin, InputIterator end);

Deadly 提交于 2019-12-12 13:35:19
问题 Given such a code segment: #include <iostream> #include <iterator> #include <fstream> #include <string> using namespace std; int main(){ ifstream file("1.txt"); string str((istream_iterator<char>(file)),istream_iterator<char>()); file.close(); cout<<str<<endl; } The code constructs a string from a file using istream_iterator. Notice that the first parameter of string constructor is enclosed with a pair of parentheses. If I omit the parentheses, there will be an error. In VC++ 2008, a link

Lvalue istringstream Required for istream_iterator?

痞子三分冷 提交于 2019-12-12 03:28:33
问题 Given a string foo in Visual Studio I can break the words into a vector by doing: vector fooVec{ istream_iterator<string>(istringstream(foo)), istream_iterator<string>() }; But this won't compile in gcc 5.1. I get the error: invalid initialization of non-const reference of type std::istream_iterator<std::basic_string<char> >::istream_type& {aka std::basic_istream<char>& } from an rvalue of type std::basic_istream<char> Now I know that gcc had a bug that was fixed by our own Jonathan Wakely.

Using istream_iterator and reading from standard input or file

元气小坏坏 提交于 2019-12-06 02:46:10
问题 I'm writing in Microsoft Visual C++ and I'd like my program to either read from standard input or a file using the istream_iterator . Googling the internets hasn't shown how simple I think it must be. So for example, I can write this pretty easily and read from standard input: #include <iostream> #include <string> #include <iterator> using namespace std; int main() { istream_iterator<string> my_it(cin); for (; my_it != istream_iterator<string>(); my_it++) printf("%s\n", (*my_it).c_str()); }

Limiting the range for std::copy with std::istream_iterator

烈酒焚心 提交于 2019-12-04 15:35:11
问题 I have constructed a minimal working example to show a problem I've encountered using STL iterators. I'm using istream_iterator to read floats s (or other types) from a std::istream : #include <iostream> #include <iterator> #include <algorithm> int main() { float values[4]; std::copy(std::istream_iterator<float>(std::cin), std::istream_iterator<float>(), values); std::cout << "Read exactly 4 floats" << std::endl; // Not true! } This reads all possible floats s, until EOF into values , which

Using istream_iterator and reading from standard input or file

痴心易碎 提交于 2019-12-04 05:20:38
I'm writing in Microsoft Visual C++ and I'd like my program to either read from standard input or a file using the istream_iterator . Googling the internets hasn't shown how simple I think it must be. So for example, I can write this pretty easily and read from standard input: #include <iostream> #include <string> #include <iterator> using namespace std; int main() { istream_iterator<string> my_it(cin); for (; my_it != istream_iterator<string>(); my_it++) printf("%s\n", (*my_it).c_str()); } Or I can write this and read from a file: #include <iostream> #include <string> #include <iterator>

Limiting the range for std::copy with std::istream_iterator

僤鯓⒐⒋嵵緔 提交于 2019-12-03 09:38:08
I have constructed a minimal working example to show a problem I've encountered using STL iterators. I'm using istream_iterator to read floats s (or other types) from a std::istream : #include <iostream> #include <iterator> #include <algorithm> int main() { float values[4]; std::copy(std::istream_iterator<float>(std::cin), std::istream_iterator<float>(), values); std::cout << "Read exactly 4 floats" << std::endl; // Not true! } This reads all possible floats s, until EOF into values , which is of fixed size, 4, so now clearly I want to limit the range to avoid overflows and read exactly/at