1、在main()函数中,结尾不含返回值,默认为return 0
2、C++中通常能使用回车的地方都能够使用空格,反之同样
3、C++ 与C的不同之一, C++中不一定每个变量都要在函数的开始位置声明,只要保证在第一次使用之前进行声明就可以,然而在C语言中 必须先全部声明然后才能开始使用。
4、4种使用名空间的方法,
1. 在函数体外面使用, 全体函数都可以使用名空间的内容
2. 在函数内部使用,当前函数使用有效
3. 使用如 using std:cout 这样的方法 ,激活对应的空间函数
4. std:cout的使用方法
5、在C++的变量中,__XX 两个下划线开头和 _A下划线和大写字母打头的名称保留给系统资源使用。_x保留给全局标识符。
6、in被设置为计算机运行时候效率最高的长度,若是没有其他原因,应该使用int
7、变量以零开头 如 042应该视为是8进制的数
8、在cout的输出前修改格式
cout<< hex <<19 ;// 将会以16进制输出
cout<< oct <<19 ; //以8进制的形式输出
9、数字后面的U u L 指定存储方式
10、
变量的类型转换 等式 A=B+C
- B C 变量中要是有一个是浮点型,则将另外一个也转成浮点型
cout << "(long double)1+ (float)1) :" << typeid((long double)1 + (float)1).name() << endl;
cout << "(long double)1+ 1) :"<<typeid((long double)1+1).name() << endl;
cout << "(double)1+ 1) :" << typeid((double)1 + 1).name() << endl;
cout << "(float)1+ 1) :" << typeid((float)1 + 1).name() << endl;
2. bool 、char 、short、 unsigned char、 unsigned short 类型相加,先转为 int 类型再相加。无论B C 中是否有 int ,这种做法的目的是为了获得更好的计算速度。有比int 更高类型的直接转为更高类型。
cout << "(bool)1 + (short)1 :" << typeid((bool)1 + (short)1).name() << endl;
cout << "(char)1 + (short)1 :" << typeid((char)1 + (short)1).name() << endl;
cout << "(char)1 + (unsigned short)1 :" << typeid((char)1 + (unsigned short)1).name() << endl;
cout << "(unsigned char)1 + (short)1 :" << typeid((unsigned char)1 + (short)1).name() << endl;
cout << "(unsigned char)1 + (unsigned short)1 :" << typeid((unsigned char)1 + (unsigned short)1).name() << endl;
cout << "(long)1 + (short)1 :" << typeid((long)1 + (short)1).name() << endl;
3. 若 BC 为有符号和无符号的加减 ,有符号的取值能包含所有无符号的值,则转为有符号数的类型。若不能不包含,则转高级类型的无符号表示
cout << "(unsigned short)1+(int)1 :" << typeid((unsigned short)1 + (int)1).name() << endl;
cout << "(int)1 + (long int)1 :" << typeid((int)1 + (long int)1).name() << endl;
cout << "(unsigned int)1 + (long int)1 :" << typeid((unsigned int)1 + (long int)1).name() << endl;
cout << "(unsigned int)1 + (long long int)1 :" << typeid((unsigned int)1 + (long long int)1).name() << endl;
cout << "(int)1 + (unsigned long int)1 :" << typeid((int)1 + (unsigned long int)1).name() << endl;
cout << "sizeof(int) :"<< sizeof(int) << endl;
cout << "sizeof(long int) :"<< sizeof(long int) << endl;
cout << "sizeof(long long int) :"<<sizeof(long long int) << endl;
其中第三个 之所以没有转成 long int 类型 ,从后面 的long int 和int 的长度可以看出两个的长度是一样的。
4. 若两个数都是有符号或者无符号,则转为级别更高的类型
cout << "(short)1+(int)1 :" << typeid((short)1 + (int)1).name() << endl;
cout << "(short)1+(long )1 :" << typeid((short)1 + (long int)1).name() << endl;
cout << "(unsigned short)1+(unsigned int)1 :" << typeid((unsigned short)1 + (unsigned int)1).name() << endl;
cout << "(unsigned short)1+(unsigned long )1 :" << typeid((unsigned short)1 + (unsigned long int)1).name() << endl;
cout << "(int)1+(long int)1 :" << typeid((int)1 + (long int)1).name() << endl;
cout << "(unsigned int)1+(unsigned long )1 :" << typeid((unsigned int)1 + (unsigned long int)1).name() << endl;
11、强制类型转换的两种形式。
int (number) //纯粹的C++
int() number //来自C 语言