<< and >> in C++ [duplicate]
Possible Duplicate:
<< and >> in开发者_如何转开发 C++
I don't quite understand what this means...I'm just learning C++ from my very very very basic Python experience...and so this may be a very stupid question. My question is...say you have your classic "Hello World" program and you have the line:
cout<<"Hello World!"<<endl;
what does the << mean...because I was just looking at using input in C and saw that you'd do something like:
int i;
cin>>i;
and I noticed that it has >> instead of << and I've read that those are bitwise shifts...and I don't exactly understand what those are...but I think it might be different here...Help...Thanks in advance
Look up C++ operator overloading. C++ allows you to overload certain operators (such as arithmetic operators like +
, -
or *
) to provide certain functionality to user-defined classes, such as:
Foo x = 100;
Foo y = 200;
x = x + y;
The built-in C++ IOstreams library is meant to replace the C stdio.h
library functions like printf
. It overloads the <<
and >>
operators to mean "insert into a stream" and "extract from a stream" respectively. So, saying:
std::cout << "Hello world";
...will insert the string "Hello World"
into the standard output stream cout
, which is generally associated with console output. IO Streams can be used to print something to the screen, write data to a file, insert data into a string buffer, and can be extended for many other purposes (sockets, pipes, etc.)
They're indeed bitwise shifts. Numbers in computers are represented in binary form.
Example: 10 = 1010 (8x1 + 4x0 + 2x1 + 1x0).
Now, a shift just moves all numbers to the right or left.
Left shift:
10100 and that's (16x1 + 8x0 + 4x1 + 2x0 + 1x0) or 20. You multiplied by two!
Right shift:
101 (4x1 + 2x0 + 1x0) or 5. You divided by two!
It's really just another way to divide or multiply by 2.
Now, they're all so used to pump data in a graphical way.
The data goes from your input, cin to i:
int i;
cin>>i;
And the data goes from "Hello world" to the output, cout:
cout<<"Hello World!"<<endl;
精彩评论