"<<" ?

in cout what exactly is "<<"

1
2
3
4
5
6
7
8
9
10
 #include <iostream>
 #include <cstdio>

 int main(int argc, const char *argv)
 {
  std::cout<<"Hello, World!"<<std::endl;
  getchar();
 
  return 0;
 }
if your going to do C++ then you shoud know what "<<" is
if your going to do C++ then you shoud know what "<<" is

That's one of the most useful answers I've ever heard. Congrats, David!

'<<' is the bitwise left shift operator: http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/
But in the case of std::cout it's overloaded: http://www.cplusplus.com/reference/ostream/ostream/operator%3C%3C/
Last edited on
For "cout", "<<" means output. For numbers, "<<" means Shift operations. For class, you can define it yourself.("cout" is a class witch define in the file "iostream.h")
<< is a bitwise left shift operator. It is overloaded to work differently with ostream and derived classes. Standard library provides overloads fo all built-in types and several calsses from standard library (std::string for example). You can also provide overload for your own classes.
Typical overload looks like:
1
2
3
4
5
std::ostream& operator<<(std::ostream& lhs, T const& rhs)
{
    //Do output here
    return lhs;
}
Where T is your type.
You need to return stream to allow for operator chaining. Suppose you have following code:
 
std::cout << "x" << 2;
As operator << has left-to-right associativity, it could be imagined as:
 
operator<<( operator<<(std::cout, "x"), 2);
Result of inner operator<< call is std::cout, so outer one will work fine.
Last edited on
Topic archived. No new replies allowed.