01_cout

发布时间 2023-09-22 18:36:36作者: 爱吃冰激凌的黄某某

cout

cout基本使用

# include <iostream> //< >包含系统头文件 input output straam

using namespace std; //使用命名空间std
cout << "Hello World!中文" << endl; //cout输出设备(终端,屏幕),endl换行
//不使用命名空间的话:
std::cout << "Hello World!中文" << std::endl; //很麻烦
//cout默认以十进制输出
cout << 0b001010 << endl; //10
cout << 0123 << endl; //83
cout << 0x58 << endl; //88

cout输出二进制数

//cout需要使用bitset<位数>(数值), 位数一般是8,16,32
#include<bitset>
cout << bitset<8>(0b001010) << endl; //00001010

cout输出八进制数

//cout需要使用oct输出八进制
cout << oct << 0123 << endl; //123

cout输出十六进制

//cout需要使用hex输出八进制
cout << hex << 0xab << endl; //ab

cout带变量输出

int a = 20;
cout << "a = " << a << endl; //a = 20

cout输出字符

cout << 'a' << endl; //a
cout << (int)'a' << endl; //97

cout看类型输出