iostream

Why can't std::ostream be moved?

心不动则不痛 提交于 2019-11-26 12:25:37
问题 Clearly, streams can\'t be copied. It should be possible to move streams. According to 27.9.1.11 [ofstream.cons] paragraph 4 it is possible to move construct an std::ofstream (the same is true for std::ifstream , std::fstream , and the std::*stringstream variants). For example: #include <iostream> #include <fstream> #include <string> std::ofstream makeStream(std::string const& name) { return std::ofstream(name); } int main() { std::ofstream out{ makeStream(\"example.log\") }; } Trying to move

Are there any tricks to use std::cin to initialize a const variable?

梦想与她 提交于 2019-11-26 12:13:46
问题 Common std::cin usage int X; cin >> X; The main disadvantage of this is that X cannot be const . It can easily introduce bugs; and I am looking for some trick to be able to create a const value, and write to it just once. The naive solution // Naive int X_temp; cin >> X_temp; const int X = X_temp; You could obviously improve it by changing X to const& ; still, the original variable can be modified. I\'m looking for a short and clever solution of how to do this. I am sure I am not the only one

Which C I/O library should be used in C++ code? [closed]

白昼怎懂夜的黑 提交于 2019-11-26 11:48:58
In new C++ code, I tend to use the C++ iostream library instead of the C stdio library. I've noticed some programmers seem to stick to stdio, insisting that it's more portable. Is this really the case? What is better to use? Loki Astari To answer the original question: Anything that can be done using stdio can be done using the iostream library. Disadvantages of iostreams: verbose Advantages of iostreams: easy to extend for new non POD types. The step forward the C++ made over C was type safety. iostreams was designed to be explicitly type safe. Thus assignment to an object explicitly checked

Can you explain the concept of streams?

风流意气都作罢 提交于 2019-11-26 11:45:33
问题 I understand that a stream is a representation of a sequence of bytes. Each stream provides means for reading and writing bytes to its given backing store. But what is the point of the stream? Why isn\'t the backing store itself what we interact with? For whatever reason this concept just isn\'t clicking for me. I\'ve read a bunch of articles, but I think I need an analogy or something. 回答1: The word "stream" has been chosen because it represents (in real life) a very similar meaning to what

<iostream> vs. <iostream.h> vs. “iostream.h”

℡╲_俬逩灬. 提交于 2019-11-26 11:24:28
问题 When including a header file in C++, what\'s the difference between... 1) including the .h versus not including the .h when wrapping it in < > signs? #include <iostream> vs. #include <iostream.h> 2) wrapping the header name in double quotes versus wrapping it in < > signs? #include <iostream.h> vs. #include \"iostream.h\" Thanks in advance! 回答1: In short: iostream.h is deprecated - it is the original Stroustrup version, and iostream is the version from the standards committee. Generally

Restore the state of std::cout after manipulating it

亡梦爱人 提交于 2019-11-26 11:15:43
Suppose I have a code like this: void printHex(std::ostream& x){ x<<std::hex<<123; } .. int main(){ std::cout<<100; // prints 100 base 10 printHex(std::cout); //prints 123 in hex std::cout<<73; //problem! prints 73 in hex.. } My question is if there is any way to 'restore' the state of cout to its original one after returning from the function? (Somewhat like std::boolalpha and std::noboolalpha..) ? Thanks. Stefan Kendall you need to #include <iostream> or #include <ios> then when required: std::ios_base::fmtflags f( cout.flags() ); //Your code here... cout.flags( f ); You can put these at the

Do I have to use #include <string> beside <iostream>?

风格不统一 提交于 2019-11-26 09:45:35
问题 I started learning C++ and I read a book which writes that I must use the <string> header file because the string type is not built directly into the compiler. If I use the <iostream> I can use the string type. Do I have to include the <string> header when I want to use the string type if I included the <iostream> header? Why? Is there some difference? 回答1: Yes, you have to include what you use. It's not mandated that standard headers include one another (with a few exceptions IIRC). It might

How do the stream manipulators work?

心不动则不痛 提交于 2019-11-26 09:37:28
问题 It is well known that the user can define stream manipulators like this: ostream& tab(ostream & output) { return output<< \'\\t\'; } And this can be used in main() like this: cout<<\'a\'<<tab<<\'b\'<<\'c\'<<endl; Please explain me how does this all work? If operator<< assumes as a second parameter a pointer to the function that takes and returns ostream & , then please explain my why it is necessary? What would be wrong if the function does not take and return ostream & but it was void

Why are C++ STL iostreams not “exception friendly”?

ぐ巨炮叔叔 提交于 2019-11-26 09:36:26
问题 I\'m used to the Delphi VCL Framework, where TStreams throw exceptions on errors (e.g file not found, disk full). I\'m porting some code to use C++ STL instead, and have been caught out by iostreams NOT throwing exceptions by default, but setting badbit/failbit flags instead. Two questions... a: Why is this - It seems an odd design decision for a language built with exceptions in it from day one? b: How best to avoid this? I could produce shim classes that throw as I would expect, but this

How to solve “Unresolved inclusion: <iostream>” in a C++ file in Eclipse CDT?

[亡魂溺海] 提交于 2019-11-26 09:28:56
问题 I download eclipse for c++ (cdt-master-8.0.2.zip). When I write: #include <iostream> It marks: Unresolved inclusion: <iostream> How can I fix it? 回答1: Go to Project > Properties > C/C++ General > Preprocessor Includes... > Providers and select " CDT GCC Built-in Compiler Settings ". 回答2: I use Eclipse for cross compiling and I have to add the explicit directories for some of the standard C++ libraries. Right click your project and select Properties. You'll get the dialog shown in the image.