In C ++, is `var << ifstream` the same as` ifstream >> var`?

Is it var << ifstreamthe same as ifstream >> var?

As far as I can tell, they should be exactly the same. But it's late, and my brain is half to death, so I would like to clarify.

+3
source share
6 answers

They are not the same. foo << bar- foo.operator<<(bar)or operator<<(foo, bar), a bar >> foo- bar.operator>>(foo)or operator>>(bar, foo).

These are just different things. Regardless of whether any of these versions exist, not to mention whether there are two versions, they do the same, completely depend on what is in your code.

iostreams, , T :

std::ostream & operator<<(std::ostream &, T const &);  // for "os << x"
std::istream & operator>>(std::istream &, T &);        // for "is >> y"
+9

. . operator >>, - operator <<. .

, F(int,double) , Q(double, int) - , , - , - . , , .

+3

var << , .

+2

, , . . :

var << ifstream;

var.operator<<(ifstream);

ifstream >> var;

ifstream.operator>>(var);

: , , . → :

returntype operator>>(ifstream, var);
returntype operator>>(var, ifstream);

, -.

.

0

, . , << >> , / . :

std::cout << "Hello" << ',' << " world";

( istream s). : << -,

((std::cout << "Hello") << ',') << " world";

std::cout                         :   std::ostream
(std::cout << "Hello")            :   std::ostream
((std::cout << "Hello") << ',')   :   std::ostream

a operator<<(stream, value), . , , ,

" world" >> ',' >> "Hello" >> std::cout;

, , , . ,

((" world" >> ',') >> "Hello") >> std::cout;

operator>>(const char*, char). , , std::ostream?

0

, , - ifstream , operator>>.

-1

All Articles