tags:

views:

132

answers:

7

I feel pretty stupid, but I'm just starting to learn c++ after coming from other languagues, and I cannot for the life of me comprehend statements that use "<<" and ">>". I know it is simple, but it confuses me every time.

The book I'm using made a really good recommendation to read const declarations from right to left. Is there any similar method for "<<" and ">>"?

A: 

This is easy enough the object that the symbol is pointing to on the receiving end. So:

cout << "Enter First Name: ";

    cin >> FirstName;

in this code, cout in usually stdout and is "receiving" the characters "Enter First Name", FirstName which is presumably a variable is "receiving" the data from cin which is probably stdin.

ennuikiller
+2  A: 

i like to say "gets".

cout << "some expression = " << expr << endl;

i'd say that as "cout gets some expression, gets expr, gets endull." similarly if you're reading from a stream, it's your variable that "gets" a value from the input...

Robert Karl
+1  A: 

I typically read them from left to right, like so:

cout << setw(6) << val << endl;

"Send to cout, with a width of 6, the contents of val, and then end the line and flush."

or

cin >> val;

"Get from cin the contents of val."

It does take a little bit of special consideration in that you can't just say specific words for each token.

Amber
+2  A: 

On the phone, back when I was a consultant frequently needing to "read aloud" C++ code to clients' employees on the phone, I (explaining once or twice, then they got it) read "input" operator >> as "absorb" and "output" operator << as "emit". "get" and "put" would work just as well if they didn't conflict with alas-too-common other uses. But that doesn't seem to have much to do with right-to-left vs left-to-right, just with your Q's title about "reading aloud" -- "cin absorb x" or "essteedee cout emit blach emit essteedee endl" kinds of "reading code aloud" (e.g. on the phone)!-)

Alex Martelli
+1  A: 

To understand C++, you have to understand that C++ has many of the most powerful tools to build abstractions. cout is nothing but a stream. So, it is very acceptable to mimic the concept of stream.

For example,

std::cout << "hey"; // '<<' is something like the direction of the `data`

throws things in the stream with the same syntax and conventions, infact there is only one syntax for all types.

Also,

std::cin >> number;

extracts the next value of the type specified by number and put it in number.

It just make sense when you think about streams in C++. You can see this in most modern C++ libraries, for example with Boost you can do this:

boost::filesystem::path mypath;
....
mypath = mypath/filename;

The division operator is replaced with '/' or '\' depending on the system you are targeting :)

AraK
A: 

A simple way to distinguish the two is like so.

cout << "Some fancy string...";

In this example your are taking a string and sending it to the world (monitor).

cin >> x;

In that one you take an value from the world (keyboard) and save the it into x. Hence the direction Value >> X.

Absolute0
A: 

I never thought too much about it but I suppose you could say "Streams to" or "Streams from" eg.

cout << mysting << endl;

"cout streams from mystring streams from endline"

cin >> myvalue;

"cin streams to myvalue"

I just made that up but it makes sense to me.

Adam Pierce