C++学习总结

1、不同的数据类型本质区别就是在内存中占的空间大小不同,即占据的字节个数不一样。数据以字节为单位存储在存储器中。
整型:短整型short–2字节 整型int–4字节 长整型long–4字节 长长整型long long–8字节

	short num1 = 10;
	int   num2 = 10;
	long  num3 = 10;
	long long num4 = 10;
	
	cout <<" num1 ="<<num1<<" short 短整型字节数为:"<<sizeof(num1)<<"字节"<< endl;
	cout << " num2 =" << num2 << " int  整型字节数为:" << sizeof(num2) << "字节" << endl;
	cout << " num3 =" << num3 << " long 长整型字节数为:" << sizeof(num3) << "字节" << endl;
	cout << " num4 =" << num4 << " long long 长整型字节数为:" << sizeof(num4) << "字节" << endl;

浮点型:分为单精度float与双精度double,两者之间的本质区别就是在内存空间中占的字节不一样
单精度float:4字节–7有效数字
双精度double:8字节–15-16有效数字

float  f1 = 3.14f;
	double f2 = 3.14;
	cout << " f1 =" << f1 << " 单精度float型字节数为:" << sizeof(f1) << "字节" << endl;
	cout << " f2 =" << f2 << " 双精度double型字节数为:" << sizeof(f2) << "字节" << endl;

char型:char型只是把字符对应的ASCII码存储在寄存器中而无法将将字符本身存储在寄存器中

	char ch1 = 'a';//必须加单引号
	
	cout << " ch1 =" << ch1 << " char型字节数为:" << sizeof(ch1) << "字节" << endl;

\t水平制表符,占8个字节,用来水平值表,\告诉编译器我接下来要输入特殊字符
\t水平制表符,占8个字节,用来水平值表,\告诉编译器我接下来要输入特殊字符
字符串:两种形式
1、c语言:char str1[] =“hello”;
1、c++:string str2 =“world”;//需要包含#include

	char str1[] = "hello";
	string str2 = "world";
	cout <<str1<<" "<<str2<< endl;

//输出cin>>要输入至目标

	string str3 = "chengjiagen";
	cout <<"请输入一个字符串: "<< endl;
	cin >> str3;
	cout <<str3<< endl;

递增运算分为前置递增与后置递增,并且还分是否带表达式,若不带表达式则两者没有区别,若带表达式则有区别。
递增(减)分为
1、前置递增(减) ++a (–a) ----先进行变量的自增运算然后再计算表达式(若有表达式的话)
1、后置递增(减) a++ (a++) ----先计算表达式然后再将变量进行自增运算(若有表达式的话)

注:实际上递增并且带表达式是一种简化的写法,比如:

a++ * b;
实际为
a* b;
a++ ;

++ a* b;
实际为
a++ ;
a* b;

//没有表达式,两者没有区别
	cout << "前置递增  与  后置递增" << endl;
	int aa = 10;
	int bb = 10;
	cout << "aa =" << aa << endl;
	cout << "bb =" << bb << endl;
	aa++;
	++bb;
	cout << "没有表达式,两者没有区别" << endl;
	cout << "aa++ =" << aa << endl;
	cout << "++bb =" << bb << endl;

	//有表达式,两者有区别
	cout << "有表达式,两者有区别" << endl;
	int cc = aa++ * bb;
	int dd = ++aa * bb;

	cout << "cc = aa++ * bb =" << cc << endl;
	cout << "dd = ++aa * bb =" << dd << endl;

要区分按位或与逻辑或

	cout << "按位或-----|  与  逻辑或-----||" << endl;
	cout << "a | b =" << (a | b) << endl;
	cout << "a  || b =" << (a || b) << endl;

此处注意cout << endl;输出则会进行换行,若不需要换行,则不需要加<<endl;

//打印乘法表
	cout << "打印乘法表" << endl;
	for (int i = 1; i < 10; i++)
	{
		for (int j = 1; j <= i; j++)
		{
			cout << j << "*" << i << "=" << i * j <<"\t"; //注意此时没有加上endl,加上endl会自动换行
		}
		cout <<  endl;
	}

//break------1、用于跳出最近内层嵌套循环(不会影响外层循环) 2、跳出选择语句 3、跳出switch语句

	//跳转语句
	cout << "跳转语句" << endl;
	//break------1、用于跳出最近内层嵌套循环(不会影响外层循环)  2、跳出选择语句  3、跳出switch语句
	int Select = 0;
	cout << "请选择一个数字1-3";
	cin  >> Select;
	cout << "跳出switch语句" << endl;
	switch (Select)
	{
	case  1: cout << 1 << endl;	break;
	case  2: cout << 2 << endl; break;
	case  3: cout << 3 << endl; break;
	}
	cout << "跳出选择语句" << endl;
	for (int i = 0; i < 6; i++)
		{
		cout << i << endl;
		if (i == 3)
			break;

		}
	cout << "跳出最近内层嵌套循环(不会影响外层循环)" << endl;

	for (int i = 1; i < 10; i++)
	{
		for (int j = 1; j <= i; j++)
		{
			cout << j << "*" << i << "=" << i * j << "\t"; //注意此时没有加上endl,加上endl会自动换行
			if (j == 3)
				break;
		}
		cout << endl;
	}

continue—作用:在循环语句中,跳出本次循环中余下尚未执行的语句,继续本层循环中的执行下一次的循环
continue与break的区别:continue不会终止本次循环,break会终止本次循环

		cout << "continue语句" << endl;
		for (int i = 0; i < 100; i++)
		{
			if (i%2 == 0)
				continue;
			cout << i << " ";
		}

一维数组,数组名是一个地址、是一个常量不可修改!

		int arr[100] = {1,2,3,4,5,6,7,8,9};
		cout << "一维数组 "<< endl;
		cout << "数组在内存中占据的内存空间:" << sizeof(arr) << endl;
		cout << "数组中元素占据的内存空间:" << sizeof(arr[0]) << endl;
		cout << "数组中元素的个数:" << sizeof(arr) / sizeof(arr[0]) << endl;
		cout << "数组首地址:" << arr << endl;
		cout << "数组第一个元素的地址:" << &arr[0] << endl;
		cout << "数组第二个元素的地址:" << &arr[1] << endl;
		//arr = 100;//错误,数组名是一个地址、是一个常量不可修改!
		
		

	

找出数组中最大的数 1、定义一个最大数的变量 2、遍历每个元素,比较之


		cout << "找出数组中最大的数 1、定义一个最大数的变量 2、遍历每个元素,比较之" << endl;
		int max = 0;//C++局部变量必须初始化!
		for (int i = 0; i < 100; i++)
		{
			if (max < arr[i])
				max = arr[i];
		}
		cout << max << endl;

//数组元素互换
	/*******************************************
     1、定义首位标识start,与末尾标识end
	 2、定义中间变量
	 3、交换元素
	 4、更新标识start与end
	 5、循环
	*********************************************/

		cout << "数组元素首尾互换" << endl;
		int array[5] = { 1,2,3,4,5 };
		cout << "数组元素首尾互换前:" << endl;
		for (int i = 0; i < 5; i++)
		{
		cout << array[i] << " ";
		}
		cout << endl;

		int start = 0;
		int end = sizeof(array) / sizeof(array[0]) - 1;
		int temp;

		while (start < end)
		{
			temp = array[start];
			array[start] = array[end];
			array[end] = temp;

			start++;
			end--;
		}
		cout << "数组元素首尾互换后:" << endl;
		for (int i = 0; i < 5; i++)
		{
			cout << array[i] << " ";
		}
		cout << endl;
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值