istream-iterator

How to read arbitrary number of values using std::copy?

Hi, I'm trying to code opposite action to this: std::ostream outs; // properly initialized of course std::set<int> my_set; // ditto outs << my_set.size(); std::copy( my_set.begin(), my_set.end(), std::ostream_iterator<int>( outs ) ); it should be something like this: std::istream ins; std::set<int>::size_type size; ins >> size; s...

what is the result of incrementing an istream_iterator which is already at the end of the stream?

I've looked at the standard and didn't see an obvious answer. suppose i've done this: std::istream_iterator<char> is(file); while(is != std::istream_iterator<char>()) { ++is; } now is is at the end of the stream and is equal to std::istream_iterator<char>(). What happens if I increment it one more time? Is it still equal to std::...

C++ compilation error using string and istream_iterator

When trying to compile the following: #include <string> #include <iterator> #include <iostream> using namespace std; int main() { string s(istream_iterator<char>(cin), istream_iterator<char>()); return s.size(); } g++ 4.4.1 gives me: main.cc: In function ‘int main()’: main.cc:6: error: request for member ‘size’ in ‘s’, which is o...

getline vs istream_iterator

Should there be a reason to preffer either getline or istream_iterator if you are doing line by line input from a file(reading the line into a string, for tokenization). ...

Using istream_iterator and reading from standard input or file

Hi, 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>...

Why can't I construct an std::istream_iterator with an unnamed temporary?

g++ allows this construction of an istream_iterator from an ifstream instance: std::ifstream ifstr("test.txt"); std::istream_iterator<std::string> iter1(ifstr); ...but it doesn't allow the same construction with an unnamed temporary: std::istream_iterator<std::string> iter2(std::ifstream("test.txt")); This gives: error: no mat...

Copying from istream never stops

This bit of code runs infinitely: copy(istream_iterator<char>(cin), istream_iterator<char>(), back_inserter(buff)); The behavior I was expecting is that it will stop when I press enter. However it doesn't. buff is a vector of chars. ...

istream_iterator leaking memory

All right, you guys were very helpful with my last question, so I'll try another one. This is also homework and while the last one was quite old, this has been submitted and is waiting to be marked. So if there's anything that will bite me it'll probably be this problem. I've obfuscated the class names and such since it's still possible ...

Why does istream_iterator<unsigned char, unsigned char> throw std::bad_cast?

What is going on? #include <iostream> #include <iterator> #include <sstream> int main() { std::basic_stringbuf<unsigned char> buf; std::basic_istream<unsigned char> stream(&buf); // the next line throws std::bad_cast on g++ 4.4 std::istream_iterator<unsigned char, unsigned char> it(stream); } I've tried stream.write(s...

Can't instantiate an istring_iterator using a wistringstream

I'm trying to split a string using the method found in this thread, but I'm trying to adapt it to a wstring. However, I have stumbled upon a weird error. Check the code: #include <iostream> #include <sstream> #include <string> #include <vector> #include <algorithm> #include <iterator> using namespace std; int main(void) { wstring ...

copy using istream_iterator

What would be end of source in this case when getting a string input from console? int main() { std::vector<std::string> str; copy (istream_iterator<std::string>(std::cin), istream_iterator<std::string>(), std::back_inserter(str)); } ...