• 【C++学习】基础1


    C++输出Hello world

    #include 
    using namespace std;
    // main函数必须有且只有一个
    int main() {
    	// 不换行
    	//cout << "hello world"; 
    	//换行
    	cout << "hello world" << endl; 
    
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    C++注释

    • //描述信息:单行注释;

    • /*描述信息*/:多行注释;

    C++变量

    数据类型 变量名 变量初始值

    #include 
    using namespace std;
    
    int main() {
    	int a = 10;
    	cout << "a=" << a << endl;
    
    	system("pause");
    	return 0;
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    变量作用是给内存空间起名,方便操作这段内存

    C++常量

    作用:用于记录程序中不可修改的数据

    • #define宏常量:#define 常量名 常量值
      • 通常定义在cpp文件上方,表示一个常量
    • const修饰的变量:const 数据类型 常量名 = 常量值
      • 通常在变量定义前加关键字const,修饰该变量为常量,不可修改
    #include 
    using namespace std;
    #define day 7 // 宏常量
    
    int main() {
    	//day = 8 //一旦修改常量会报错
    	const int a = 10; // const修饰的变量=常量
    	//a = 11 // 一旦修改该常量也会报错 
    	cout << "day=" << day << endl;
    	cout << "a=" << a << endl;
    
    	system("pause");
    	return 0;
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    C++关键字

    知乎-C++的关键字介绍

    写变量名称的时候不要和关键词重复!!!

    C++标识符命名规则

    C++规定给标识符(变量、常量)命名时,有一套自己的规则:

    • 标识符不能是关键字
    • 标识符只能由字符、数字、下划线组成
    • 第一个字符必须为字母或者下划线(不能是数字)
    • 标识符中的字符区分大小写

    C++数据类型

    为什么会有数据类型这种东西?为了分配一个合理的内存空间

    整型

    • short:短整型、2字节
    • int:整型、4字节
    • long:长整型、Windows为4字节、Linux为4字节(32位),8字节(64位)
    • long long:长长整型、8字节
    #include 
    using namespace std;
    
    int main() {
    	// 短整型(可以表示-2^15到2^15-1)
    	short num1 = 10;
    	//2^15-1=32767,假如num1=32768就会输出负数,因为它表示不了那么大
    	//short num1 =32768 ;
    	// 整型(可以表示-2^31到2^31-1)
    	int num2 = 10;
    	// 长整型(可以表示-2^31到2^31-1)
    	long num3 = 10;
    	// 长长整型(可以表示-2^63到2^63-1)
    	long long num4 = 10;
    	cout << num1 << endl;
    	cout << num2 << endl;
    	cout << num3 << endl;
    	cout << num4 << endl;
    	system("pause");
    	return 0;
    }  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    sizeof关键字

    利用sizeof可以可以统计数据类型所占用的内存的大小,sizeof(数据类型/变量)

    #include 
    using namespace std;
    
    int main() {
    	short num1 = 10;
    	int num2 = 10;
    	long num3 = 10;
    	long long num4 = 10;
    	// sizeof(变量)
    	cout << sizeof(num1) << endl;
    	cout << sizeof(num2) << endl;
    	cout << sizeof(num3) << endl;
    	cout << sizeof(num4) << endl;
    	//sizeof(数据类型)
    	cout << sizeof(short) << endl;
    	cout << sizeof(int) << endl;
    	cout << sizeof(long) << endl;
    	cout << sizeof(long long) << endl;
    	system("pause");
    	return 0;
    }  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    实型(浮点型)

    用于表示小鼠

    • 单精度:float
    • 双精度:double
    数据类型占用空间有效数字范围
    float4字节7位有效数字
    double8字节15-16位有效数字

    在C中,3.14的有效数字位数为3,而不是小数点后的2个数

    #include 
    using namespace std;
    
    int main() {
    	float f1 = 3.14f; //加f指定是单精度,如果不说明,则认为是双精度数字
    	double d1 = 3.14;
    	cout << f1 << endl;
    	cout << d1 << endl;
    	cout << sizeof(float) << endl;
    	cout << sizeof(double) << endl;
    
    	// 科学计数法
    	float f2 = 3e2; // 3*10^2
    	float f3 = 3e-2; // 3*0.1^2
    	cout << f2 << endl;
    	cout << f3 << endl;
    	system("pause");
    	return 0;
    }  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    字符型

    字符型变量用于显示单个字符,char ch = 'a';

    • C和C++中的字符型变量只占用1个字符
    • 字符型变量并不是把字符放在内存中,而是把字符对应的ASCII编码放在存储单元
    #include 
    using namespace std;
    
    int main() {
    	char ch = 'a';
    	//错误1:char ch = "a"; // 不可以使用双引号
    	//错误2:char ch = 'abcdf'; //不可以有多个字符
    	cout << ch << endl;
    	cout << sizeof(ch) << endl;
    	//查看字符的ASCII的编码
    	cout << int(ch) << endl;
    	system("pause");
    	return 0;
    }  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    转义字符

    用于表示一些不能显示出来的ASCII字符

    #include 
    using namespace std;
    
    int main() {
    	//换行符 \n
    	cout << "Helo World\n"; // 等同于cout << "Helo World" << endl;
    	cout << "Helo \n World";
    	// 反斜杠\ \ 
    	 cout << "\\" << endl;
    	// 水平制表 \t
    	cout << "aaaa\tbcdef" << endl;
    	system("pause");
    	return 0;
    }  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    字符串型

    用于表示一串字符

    1. C风格的字符串:char 变量名[]=""
    #include 
    using namespace std;
    
    int main() {
    	char str[] = "hello";
    
    	cout << str<< endl;
    	system("pause");
    	return 0;
    }  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    1. C++风格的字符串:string 变量名 = ""
    #include 
    using namespace std;
    #include  // 注意这里引入了string的头文件
    
    int main() {
    	string str = "hello";
    
    	cout << str << endl;
    	system("pause");
    	return 0;
    }  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    布尔类型bool

    布尔数据类型用于表示真或者假的值。

    bool只用两个值:

    • true – 真 (本质是1)
    • false – 假 (本质是0)
    #include 
    using namespace std;
    
    int main() {
    	bool flag1 = true;
    	bool flag2 = false;
    	cout << flag1 << endl;
    	cout << flag2 << endl;
    	cout << sizeof(bool) << endl;
    	system("pause");
    	return 0;
    }  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    数据的输入

    从键盘获取值,cin >> 变量

    #include 
    using namespace std;
    #include 
    
    int main() {
    	// 整型
    	int a = 0;
    	cout << "请给a赋值:" << endl;
    	cin >> a;
    	cout << a;
    
    	// 浮点型
    	float b = 0.1;
    	cout << "请给b赋值:" << endl;
    	cin >> b;
    	cout << b;
    
    	// 字符型
    	char c = 'a';
    	cout << "请给c赋值:" << endl;
    	cin >> c;
    	cout << c;
    
    	// 字符串型
    	string d = "ab";
    	cout << "请给d赋值:" << endl;
    	cin >> d;
    	cout << d;
    
    	// bool型
    	bool e = true;
    	cout << "请给e赋值(0或1):" << endl;
    	cin >> e;
    	cout << e;
    	system("pause");
    	return 0;
    }  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
  • 相关阅读:
    【数据结构】双向链表(C语言)
    人类高质量代码—Golang标准库net/rpc
    Java中[Ljava.lang.Object;是什么?
    四、Shiro认证
    DockerCompose常用命令及演示
    3D孪生场景搭建:参数化模型
    session共享(redis实现)
    Kubernetes实战(三)-k8s节点设置cpu高于多少就不调度
    读《追问》
    Win:在 Windows Server 中的 NIC Teaming
  • 原文地址:https://blog.csdn.net/u014297502/article/details/126003404