跳到主要内容

cout的基本使用

cout是C++中的输出方法,流式输出

输出字符串,输出一句话

cout<<"hello";

输出换行

cout<<"hello"<<endl;
cout<<"world"<<endl;
cout<<"Hello\n";
cout<<"World\n";

输出数字

cout<<123<<endl;
cout<<1+1<<endl;
cout<<1.23<<endl;

输出字符

cout<<'a'<<endl;
cout<<'B'<<endl;

输出变量

int a=0;
cout<<a<<endl;

输出判断

//一定要括起来
cout<<(2>1)<<endl;//2大于1是对的 就会输出1
cout<<(2<1)<<endl;//2小于1是错的 就会输出0

流式输出,输出多个元素

cout<<"1+2*3="<<1+2*3<<endl;    //输出1+2*3=7
int a=2,b=3;
cout<<a<<"*"<<b<<"="<<a*b<<endl;//输出2*3=6
cout<<"a\nb"<<endl<<"c"<<endl;//endl不一定只能加在末尾