iostream

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

一笑奈何 提交于 2019-11-30 07:22:52
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? 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://stackoverflow.com/questions/4512631/difference-between-endl-and-n

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

浪尽此生 提交于 2019-11-30 06:49:54
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 error } I compile this program with gcc and get the typical 100 lines of errors. The relevant part, I

cstdio streams vs iostream streams?

一世执手 提交于 2019-11-30 06:12:13
I just learned of the existence of the ios_base::sync_with_stdio function, which basically allows you to turn off (or on if you already turned it off) the synchronization between iostream streams that are used in C++ and the cstdio streams that are part of Standard C. Now, I always thought that stdout , stderr and stdin in C were essentially wrapped in a set of objects in C++ in the iostreams classes. But if they have to be synchronized with each other, this would indicate that C++'s iostream classes are not a wrapper around C's stdin etc. I'm quite confused by this? Can someone clarify how C+

While loop with try catch fails at bad cin input

徘徊边缘 提交于 2019-11-30 05:59:55
问题 I can't seem to figure out why this falls into a loop after getting non-int input. I've tried cin.flush(), which doesn't seem to exist, cin.clear(), which seems like it should work, even cin.sync() after reading someone else post about it working, but didn't seem to make much sense. Also tried cin.bad(). Thank you very much for any help Please enter the first number: f Sorry, I don't think that's a number? Please enter the first number: Sorry, I don't think that's a number? Please enter the

Difference between ios::app and ios::ate [duplicate]

房东的猫 提交于 2019-11-30 04:48:27
Possible Duplicate: C++ Filehandling: Difference between ios:app and ios:ate? What is the difference between these two file opening modes ? ios:ate sets the get/put pointer position to the end of the file => reading/writing will begin from end, but how is it different from ios::app, which again opens a file in append mode...but when I have created a ofstream and opened it in ios:app mode, the put stream pointer still points to the beginning, how does the appending work then ? Also I understand that ifstream, ofstream and fstream are high level classes to manage the underlying stream buffer. So

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

坚强是说给别人听的谎言 提交于 2019-11-30 04:34:39
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? 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 uses other iostream members/operators to do its work, it does not have to deal with creating a sentry object

Possible to stop cin from waiting input?

ぐ巨炮叔叔 提交于 2019-11-30 03:51:25
问题 In a graphical application I execute debug commands using the console input. When the console is created a new thread is also created to gather the user commands that handles all that input, the graphical application continues running parallel. I use boost::thread library. It works good so far, however I haven't found a nice solution to stop the execution of this thread. The thread is always waiting for a user input: while(appRunning) { std::cin>>theUserCommand; // ...do stuff } Then when the

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

时光毁灭记忆、已成空白 提交于 2019-11-30 03:25:53
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? 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. The prototypes of the < operator are like : ​bool T::operator <(const T& b) const; So I guess the compiler transtype the

How to store formatting settings with an IOStream?

最后都变了- 提交于 2019-11-30 03:23:20
问题 When creating formatted output for a user defined type it is often desirable to define custom formatting flags. For example, it would be nice if a custom string class could optionally add quotes around the string: String str("example"); std::cout << str << ' ' << squotes << str << << ' ' << dquotes << str << '\n'; should produce example 'example' "example" It is easy enough to create manipulators for changing the formatting flags themselves: std::ostream& squotes(std::ostream& out) { // what

Why do I need to flush my I/O stream to get the correct result?

为君一笑 提交于 2019-11-30 02:50:39
问题 Why the code below does not work? I mean, it shows all kinds of weird characters on console output. #include <stdio.h> char mybuffer[80]; int main() { FILE * pFile; pFile = fopen ("example.txt","r+"); if (pFile == NULL) perror ("Error opening file"); else { fputs ("test",pFile); fgets (mybuffer,80,pFile); puts (mybuffer); fclose (pFile); return 0; } } However, the code below works well. #include <stdio.h> char mybuffer[80]; int main() { FILE * pFile; pFile = fopen ("example.txt","r+"); if