istream

Why is istream/ostream slow

混江龙づ霸主 提交于 2019-11-27 18:31:05
At 50:40 of http://channel9.msdn.com/Events/GoingNative/2013/Writing-Quick-Code-in-Cpp-Quickly Andrei Alexandrescu makes a joke about how not efficient/slow istream is. I had an issue in the past with ostream being slow and fwrite being significantly faster (reducing many seconds when running the main loop once) but I never understood why nor looked into it. What makes istream and ostream slow in C++? or at least slow compared to other things (like fread/fget, fwrite) which would equally satisfied the needs. Dietmar Kühl Actually, IOStreams don't have to be slow! It is a matter of implementing

What's the difference between getline and std::istream::operator>>()?

落花浮王杯 提交于 2019-11-27 15:18:39
#include <iostream> #include <string> using namespace std; int main() { string username; cout<< "username" ; cin >> username; } So I was curious on what's the difference between these two codes, I heard it's the same thing but if it is then why two ways of doing it then? #include <iostream> #include <string> using namespace std; int main() { string username; cout << "username" ; getline (cin,username) ; } The difference is that std::getline — as the name suggests — reads a line from the given input stream (which could be, well, std::cin ) and operator>> reads a word 1 . That is, std::getline

C++ streams confusion: istreambuf_iterator vs istream_iterator?

牧云@^-^@ 提交于 2019-11-27 11:13:13
What is the difference between istreambuf_iterator and istream_iterator ? And in general what is the difference between streams and streambufs? I really can't find any clear explanation for this so decided to ask here. IOstreams use streambufs to as their source / target of input / output. Effectively, the streambuf-family does all the work regarding IO and the IOstream-family is only used for formatting and to-string / from-string transformation. Now, istream_iterator takes a template argument that says what the unformatted string-sequence from the streambuf should be formatted as, like

What Effect Would LWG2349 Have?

喜夏-厌秋 提交于 2019-11-27 07:45:15
问题 While libstdc++ does not, libc++ does follow the standard which states that passing ios_base::failbit to basic_istream::exceptions has no effect on formatted input. For example this code: istringstream is{"ASD"}; double foo; is.exceptions(istream::failbit); try { is >> foo; cout << foo << endl; } catch(ios_base::failure& fail) { cout << "ouch\n"; } Would result in: "ouch" on libstdc++ "0" on libc++ My reading of LWG2349 is that it would cause basic_istream to not throw on any formatted input.

FILE * and istream: connect the two?

与世无争的帅哥 提交于 2019-11-27 06:12:08
问题 Suppose I "popen" an executable, I get a FILE* in return. Furthermore, suppose I'd like to "connect" this file to an istream object for easier processing, is there a way to do this? 回答1: There is no standard way but if you want a quick solution you can get the file descriptor with fileno() and then use Josuttis' fdstream. There may be similar efforts around but I used this in the distant past and it worked fine. If nothing else it should be a very good map to implementing your own. 回答2: You

Input from stream to enum type

a 夏天 提交于 2019-11-26 23:28:56
问题 How to input from stream to enum type? I can do it so unsigned int sex = 0; stream >> sex; student.m_bio.sex = static_cast<Sex>(sex); Otherwise? 回答1: inline std::istream & operator>>(std::istream & str, Sex & v) { unsigned int sex = 0; if (str >> sex) v = static_cast<Sex>(sex); return str; } If you want to ensure that the value is valid, you can do something like this: enum Sex { Male, Female, Sex_COUNT }; inline std::istream & operator>>(std::istream & str, Sex & v) { unsigned int sex = 0;

Get an istream from a char*

这一生的挚爱 提交于 2019-11-26 21:58:15
I have a char* and the data length that I'm receiving from a library, and I need to pass the data to a function that takes an istream. I know I can create a stringstream but that will copy all the data. And also, the data will surely have 0s since it's a zip file, and creating a stringstream will take the data until the first 0 I think. Is there any way to create an istream from a char* and it's size without copying all the data? Here's a non-deprecated method found on the web , has you derive your own std::streambuf class, but easy and seems to work: #include <iostream> #include <istream>

Why is istream/ostream slow

安稳与你 提交于 2019-11-26 19:18:40
问题 At 50:40 of http://channel9.msdn.com/Events/GoingNative/2013/Writing-Quick-Code-in-Cpp-Quickly Andrei Alexandrescu makes a joke about how not efficient/slow istream is. I had an issue in the past with ostream being slow and fwrite being significantly faster (reducing many seconds when running the main loop once) but I never understood why nor looked into it. What makes istream and ostream slow in C++? or at least slow compared to other things (like fread/fget, fwrite) which would equally

What's the difference between getline and std::istream::operator>>()?

我与影子孤独终老i 提交于 2019-11-26 17:06:17
问题 #include <iostream> #include <string> using namespace std; int main() { string username; cout<< "username" ; cin >> username; } So I was curious on what's the difference between these two codes, I heard it's the same thing but if it is then why two ways of doing it then? #include <iostream> #include <string> using namespace std; int main() { string username; cout << "username" ; getline (cin,username) ; } 回答1: The difference is that std::getline — as the name suggests — reads a line from the

C++ streams confusion: istreambuf_iterator vs istream_iterator?

微笑、不失礼 提交于 2019-11-26 15:23:48
问题 What is the difference between istreambuf_iterator and istream_iterator ? And in general what is the difference between streams and streambufs? I really can't find any clear explanation for this so decided to ask here. 回答1: IOstreams use streambufs to as their source / target of input / output. Effectively, the streambuf-family does all the work regarding IO and the IOstream-family is only used for formatting and to-string / from-string transformation. Now, istream_iterator takes a template