#include <iostream>
using namespace std;
int main() {
char c;
while ( cin.get(c) ) cout<<c;
}
And
#include <iostream>
using namespace std;
int main() {
char c;
while ( cin.get(c) ) cout.put(c);
}
Difference between these two
What is the difference between the two code and which should I use?
[edit: whoops, not quite]Well, in this case they end up being exactly the same thing. In general, though, you should prefer the stream insertion operator (operator<<), as it performs formatted input (assuming this is what you want).
jfl.
jfl.
From MSDN:
cout.put() is said to put a character in the stream, and the overloaded << operator writes to the stream. Basically, cout.put() can only write a single character the the console, while cout<< will write an entire string; if you pass a string to cout.put(), you will get a compiler error.
EDIT: Beaten to it...
cout.put() is said to put a character in the stream, and the overloaded << operator writes to the stream. Basically, cout.put() can only write a single character the the console, while cout<< will write an entire string; if you pass a string to cout.put(), you will get a compiler error.
EDIT: Beaten to it...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement