iostream

Difference between “endl” and “\n” [duplicate]

天涯浪子 提交于 2019-11-29 09:34:26
问题 Possible Duplicate: C++: “std::endl” vs “\n” I'm wondering if there is any significant difference between these two ways to print newline : cout << endl; //approach1 cout << "\n"; //approach2 Is there any practical difference? 回答1: Yes, they're different. "\n" is just a string of length 1 that gets appended to stdout. std::endl , instead, is an object that will cause to append the newline character ( "\n" ) AND to flush stdout buffer. For this reason it will take more processing. 来源: https:/

How to use cin.fail() in c++ properly

眉间皱痕 提交于 2019-11-29 08:03:31
I'm writing a program where I get an integer input from the user with cin>>iUserSel; . If the user puts in a letter, the program goes to an infinite loop. I tried to prevent that with the code below, but the program goes to an infinite loop and prints out "Wrong! Enter a #!". How can I fix my program? cin>>iUserSel; while (iValid == 1) { if (cin.fail()) { cin.ignore(); cout<<"Wrong! Enter a #!"<<endl; cin>>iUserSel; }//closes if else iValid = 0; }//closes while I found some information on this at Correct way to use cin.fail() and C++ cin.fail() question , but I didn't understand how to use

Why can't I instantiate operator<<(ostream&, vector<T>&) with T=vector<int>?

廉价感情. 提交于 2019-11-29 07:14:50
问题 In thinking about C++ iterator question, I wrote this sample program: #include <vector> #include <iostream> #include <iterator> #include <algorithm> template <class T> std::ostream& operator<<(std::ostream&os, const std::vector<T>& v) { os<<"("; std::copy(v.begin(), v.end(), std::ostream_iterator<T>(os, ", ")); return os<<")"; } int main() { std::vector<int> v(3); std::vector<std::vector<int> > vv(3, v); std::cout << v << "\n"; // this line works std::cout << vv << "\n"; // this line produces

How can I make Unicode iostream i/o work in both Windows and Unix-land?

纵然是瞬间 提交于 2019-11-29 04:23:55
Note: This is a question-with-answer in order to document a technique that others might find useful, and in order to perhaps become aware of others’ even better solutions. Do feel free to add critique or questions as comments. Also do feel free to add additional answers. :) Problem #1: Console support for Unicode via streams is severely limited at the Windows API level. The only relevant codepage available for ordinary desktop applications is 65001, UTF-8. And then interactive input fails at the API level, and even output of non-ASCII characters fails – and the C++ standard library

When should I concern myself with std::iostream::sentry?

こ雲淡風輕ζ 提交于 2019-11-29 01:45:46
问题 Online references have rather brief and vague descriptions on the purpose of std::iostream::sentry . When should I concern myself with this little critter? If it's only intended to be used internally, why make it public? 回答1: Most people will never write any code that needs to deal with creating sentry objects. A sentry object is needed when/if you extract data from (or insert it into) the stream buffer that underlies the stream object itself. As long as your insertion/extraction operator

NaN ASCII I/O with Visual C++

半世苍凉 提交于 2019-11-29 01:24:20
I want to read and write NaN values from/into text files using iostream and Visual C++. When writing a NaN value, i get 1.#QNAN . But, reading it back outputs 1.0 . float nan = std::numeric_limits<float>::quiet_NaN (); std::ofstream os("output.txt"); os << nan ; os.close(); The output is 1.#QNAN . std::ifstream is("output.txt"); is >> nan ; is.close(); nan equals 1.0 . Solution Finally, as suggested by awoodland, I've come up with this solution. I chose "nan" as a string representation of a NaN. Both << and >> operators are overridden. using namespace ::std; class NaNStream { public: NaNStream

Typo with “cout < myint”. Why does it work?

自闭症网瘾萝莉.ら 提交于 2019-11-29 00:39:54
问题 I have this code and I searched for hours why it fails to print my income int const income = 0; std::cout << "I'm sorry, your income is: " < income; Until I found I missed to write << but wrote < . Why doesn't the compiler detect this and error out? I'm not sure why comparing cout makes sense? 回答1: integral constant 0 is also a null pointer constant - it can be compared to the result of ostream 's operator void * . Note that it'll fail if the constant has any value but 0. 回答2: The prototypes

The difference between cin.ignore and cin.sync

巧了我就是萌 提交于 2019-11-29 00:02:56
问题 What is the difference between cin.ignore and cin.sync ? 回答1: cin.ignore discards characters, up to the number specified, or until the delimiter is reached (if included). If you call it with no arguments, it discards one character from the input buffer. For example, cin.ignore (80, '\n') would ignore either 80 characters, or as many as it finds until it hits a newline. cin.sync discards all unread characters from the input buffer. However, it is not guaranteed to do so in each implementation.

What does the “c” mean in cout, cin, cerr and clog?

两盒软妹~` 提交于 2019-11-28 23:35:26
问题 What does the "c" mean in the cout, cin, cerr and clog names? I would say char but I haven't found anything to confirm it. 回答1: The "c" stands for "character" because iostreams map values to and from byte (char) representations. [Bjarne Stroustrup's C++ Style and Technique FAQ] 回答2: I originally guessed console , and this link confirmed it. But after seeing the quote from Stroustrup, it seems that's a misconception, and that the c stands for character . One thing in favor of that theory that

Why is the address of this volatile variable always at 1?

可紊 提交于 2019-11-28 23:09:32
I wanted to inspect the address of my variable volatile int clock; cout << &clock; But it always says that x is at address 1. Am i doing something wrong?? iostreams will cast most pointers to void * for display - but no conversion exists for volatile pointers. As such C++ falls back to the implicit cast to bool . Cast to void* explicitly if you want to print the address: std::cout << (void*)&clock; There's an operator<< for const void* , but there's no operator<< for volatile void* , and the implicit conversion will not remove volatile (it won't remove const either). As GMan says, the cv