记录
有C语言基础,初学C++,感觉跟C语言很多地方有些相通之处,也有一些变化,准备记录一下所学所理解,也为了给自己一个督促吧!
1、输入输出
C++这里用的是 cin 和 cout 函数,感觉很新奇。
# include <iostream>
using namespace std;
int main()
{
string str;
cin >> str ;
cout << str << endl;
cout << "Hello World !" << endl;
return 0;
}
输入一个字符串,回车后会把输入的字符串输出,并输出“Hello World !”。
2、转义序列
\ a 表示振铃字符,它可以使终端扬声器振铃。
# include <iostream>
using namespace std;
int main()
{
char alarm = '\a';
cout << alarm << "Dont't do that again ! \a\n";
return 0;
}
3、const限定符
# include <iostream>
using namespace std;
int main()
{
const int Months =12 ;
cout <<"One year has " << Months << " months \n" ;
return 0;
}
常量(如Months)被初始化后,其值就被固定了,编译器将不允许再修改该常量的值。
以下做法不可取
const int Months ;
Months =12 ;
如果在声明常量时没有提供值,则该常最的值将是不确定的,且无法修改。
如果以前使用过C 语言,您可能觉得前面讨论的#define 语句已经足够完成这样的工作了。但 const比
#define好。
**提示:**如果读者在学习C++之前学习过C 语言,并打算使用#define来定义符号常量,请不要这样做,而应使用const。
4、浮点数的优缺点
与整数相比,浮点数有两大优点。首先,它们可以表示整数之间的值。其次,由于有缩放因子,它们
可以表示的范围大得多。另一方面,浮点运算的速度通常比整数运算慢, 且精度将降低。
精度降低问题示例:
# include <iostream>
using namespace std;
int main()
{
float a= 2.34e+22f;
float b= a + 1.0f;
float c=b-a;
cout << "b-a = " << c << endl ;
return 0;
}
结果如下:
问题在于,2.34E +22是一个小数点左边有23位的数字。加上, 就是在第23位加1。但float类型只能表示数字中的前6位或前7 位, 因此修改第23位对这个值不会有任何影响。
5、强制类型转换
# include <iostream>
using namespace std;
int main()
{
int x,y,z;
x= 19.99+11.99;
y= (int) 19.99+(int)11.99;
z=int {19.99}+ int{11.99};
cout << "x = " << x << endl;
cout << "y = " << y << endl;
cout << "z = " << z << endl;
char ch = 'Z';
cout << "ch = " << int(ch) << endl;
cout << "ch = " << static_cast<int>(ch) << endl;
return 0;
}
6、数组
6.1 计算数组元素数量
# include <iostream>
using namespace std;
int main()
{
short things[]={1,2,3,4};
int num_elements = sizeof things / sizeof (short) ;
int num_elements = sizeof things / sizeof {short} ;//书上用的大括号,报错!
cout << num_elements <<endl;
return 0;
}
6.2 字符串
6.2.1
# include <iostream>
#include <typeinfo> //typeid().name
#include <cstring> // strlen()
using namespace std;
int main()
{
// \0相当于字符串截至,与C语言相同,计入字符串长度。
char dog[] = {'d','o','g'}; //非字符串
char cat[] = {'c','a','t','\0'}; //字符串
//char shirt_size = "S";//报错,双引号表示字符串相当于 S + \0
char shirt_size = 'S'; // 字符常量
cout << typeid(dog).name() <<endl;//输出类型为A3_c
cout << typeid(cat).name() <<endl;//输出类型为A4_c
cout << strlen(cat) <<endl; //C语言中的strlen函数也可用 ,输出字符串长度 3 ,不计入 \0。
cout << shirt_size <<endl;
return 0;
}
6.2.2 输入缺陷
原因是cin读到 \0 字符保存一次。解释如下:
6.2.3读取一行getline()
为了解决6.2.2的缺陷,可以选择使用getline()函数,函数每次读取一行,它通过换行符来确定行尾,但不保存换行符。在储存字符串时,它用空字符来替换换行符。
6.2.4 string字符串
7、结构简介 -20231211
7.1结构初始化
7.2结构数组
可以同时完成定义结构和创建结构变量的工作。为此,只需将变量名放在结束括号的后面即可:
#include <iostream>
int main()
{
using namespace std ;
struct inflatable
{
char name[20];
float volume;
double price;
}guests[2] =
{
{"Amy", 0.5, 21.99},
{"Bob",2000, 568.22}
};
cout<<"The guests " << guests[0].name << " and " << guests[1].name
<< " have a combined volume of "
<< guests[0].volume + guests[1].volume <<" cubic feet. \n";
return 0;
}
然而,将结构定义和变量声明分开,可以使程序更易于阅读和理解:
#include <iostream>
struct inflatable
{
char name[20];
float volume;
double price;
};
int main()
{
using namespace std ;
inflatable guests[2] =
{
{"Amy", 0.5, 21.99},
{"Bob",2000, 568.22}
};
cout<<"The guests " << guests[0].name << " and " << guests[1].name
<< " have a combined volume of "
<< guests[0].volume + guests[1].volume <<" cubic feet. \n";
return 0;
}
两段代码实现的效果是一样的。
7.3共用体
共 用 体 (union)是一种数据格式,它能够存储不同的数据类型,但只能同时存储其中的一种类型。也就是说,结构可以同时存储int、long和 double,共用体只能存储int、long或 double。共用体的句法与结构相似,但含义不同。
共用体常用于(但并非只能用于)节省内存。