iostream

Difference between iostream and iostream.h

烂漫一生 提交于 2019-11-26 03:40:51
问题 What is the difference between iostream and iostream.h ? 回答1: iostream.h is deprecated by those compilers that provide it, iostream is part of the C++ standard. To clarify explicitly there is no mention of iostream.h at all in the current C++ standard (INCITS ISO IEC 14882 2003). Edit: As @Jerry mentioned, not only does the current standard not mention it, but no standard for C++ mentions it. 回答2: iostream is a standard header. iostream.h is a non-standard header that was very common in pre

'printf' vs. 'cout' in C++

给你一囗甜甜゛ 提交于 2019-11-26 03:15:44
问题 What is the difference between printf() and cout in C++? 回答1: I'm surprised that everyone in this question claims that std::cout is way better than printf , even if the question just asked for differences. Now, there is a difference - std::cout is C++, and printf is C (however, you can use it in C++, just like almost anything else from C). Now, I'll be honest here; both printf and std::cout have their advantages. Real differences Extensibility std::cout is extensible. I know that people will

How to get IOStream to perform better?

牧云@^-^@ 提交于 2019-11-26 03:04:18
问题 Most C++ users that learned C prefer to use the printf / scanf family of functions even when they\'re coding in C++. Although I admit that I find the interface way better (especially POSIX-like format and localization), it seems that an overwhelming concern is performance. Taking at look at this question: How can I speed up line by line reading of a file It seems that the best answer is to use fscanf and that the C++ ifstream is consistently 2-3 times slower. I thought it would be great if we

Restore the state of std::cout after manipulating it

喜欢而已 提交于 2019-11-26 02:51:42
问题 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. 回答1: you need to #include <iostream> or #include <ios> then when required: std::ios

No matching function - ifstream open()

我与影子孤独终老i 提交于 2019-11-26 01:45:44
问题 This is the part of the code with an error: std::vector<int> loadNumbersFromFile(std::string name) { std::vector<int> numbers; std::ifstream file; file.open(name); // the error is here if(!file) { std::cout << \"\\nError\\n\\n\"; exit(EXIT_FAILURE); } int current; while(file >> current) { numbers.push_back(current); file.ignore(std::numeric_limits<std::streamsize>::max(), \'\\n\'); } return numbers; } And well, I kind of have no idea what is going on. The whole thing compiles properly in VS.

What is the C++ iostream endl fiasco?

僤鯓⒐⒋嵵緔 提交于 2019-11-26 01:41:48
I was listening to a google talk by Andrei Alexandrescu on the D programming language when he threw out a one liner about the "endl" fiasco. I just thought endl was the preferred way to signify the end of a line and flush the buffer for a stream. Why is it considered a fiasco? Should I not be using it in my code? Reposting from my comment: (I assume) He just means that many, especially new, C++ programmers use std::endl blindly instead of '\n' for newline, flushing unnecessarily frequently and potentially making the performance of their program abysmal. I.e., most people are taught that std:

Does the C++ standard mandate poor performance for iostreams, or am I just dealing with a poor implementation?

放肆的年华 提交于 2019-11-26 01:31:09
问题 Every time I mention slow performance of C++ standard library iostreams, I get met with a wave of disbelief. Yet I have profiler results showing large amounts of time spent in iostream library code (full compiler optimizations), and switching from iostreams to OS-specific I/O APIs and custom buffer management does give an order of magnitude improvement. What extra work is the C++ standard library doing, is it required by the standard, and is it useful in practice? Or do some compilers provide

Java IO implementation of unix/linux “tail -f”

独自空忆成欢 提交于 2019-11-26 01:27:19
问题 I\'m wondering what techniques and/or library to use to implement the functionality of the linux command \"tail -f \". I\'m essentially looking for a drop in add-on/replacement for java.io.FileReader . Client code could look something like this: TailFileReader lft = new TailFileReader(\"application.log\"); BufferedReader br = new BufferedReader(lft); String line; try { while (true) { line= br.readLine(); // do something interesting with line } } catch (IOException e) { // barf } The missing

What is the C++ iostream endl fiasco?

孤人 提交于 2019-11-26 01:08:13
问题 I was listening to a google talk by Andrei Alexandrescu on the D programming language when he threw out a one liner about the \"endl\" fiasco. I just thought endl was the preferred way to signify the end of a line and flush the buffer for a stream. Why is it considered a fiasco? Should I not be using it in my code? 回答1: Reposting from my comment: (I assume) He just means that many, especially new, C++ programmers use std::endl blindly instead of '\n' for newline, flushing unnecessarily

How to print (using cout) a number in binary form?

一个人想着一个人 提交于 2019-11-26 00:57:09
问题 I\'m following a college course about operating systems and we\'re learning how to convert from binary to hexadecimal, decimal to hexadecimal, etc. and today we just learned how signed/unsigned numbers are stored in memory using the two\'s complement (~number + 1). We have a couple of exercises to do on paper and I would like to be able to verify my answers before submitting my work to the teacher. I wrote a C++ program for the first few exercises but now I\'m stuck as to how I could verify