1.void 关键字
1.1 int 函数名(void) 该函数不接受任何参数
1.2 void 函数名(int) 函数没有返回值
2.数据类型(64位操作系统)
bool(1 byte)=char(1 byte) <short(2 byte)<long(4 byte)=int(4 byte)=float(4 byte)<double(8 byte)=long long(8 byte)
1 字节(byte) = 8 位(bit) = (0~255) 或 (-128 ~ 127)
2.1无符号类型
unsigned (char/short/int/long)
2.2
C++将整型常量存储为int类型
C++对字符用单引号,对字符串使用双引号。
符号整型(bool、char、signed char、unsigned char、short、unsigned short 、int 、unsigned int 、long、unsigned long)
浮点型(float、double、long double)
2.3 求模运算符(%)
求模运算返回整数除法的余数
2.4类型装换
typeName (value)
static_cast<typeName> (value)
3.复合类型(64位操作系统)
3.1
sizeof 运算符作用于数组名,得到整个数组中的字节数
sizeof用于数组元素,得到是元素的长度
3.2 字符串以空字符结尾,空字符被写作\0。
3.3 读取字符串函数 getline() 和 get()
getline() 丢弃换行符 get() 将换行符保留
或者
#include "stdafx.h"
#include<iostream>
#include<cmath>
#define ZERO 0
#include<climits>
using namespace std;
int main()
{
const int ArSize = 20;
char name[ArSize];
char dessert[ArSize];
cout << "请输入你的名字" << endl;
cin.get(name, ArSize).get();
cout << "请输入你喜欢的甜点" << endl;
cin.get(dessert, ArSize).get();
cout << "我有美味甜品的店" << dessert << "推荐给" << name << endl;
}
问题1:
(没有输入地址信息)(因为cin读取年份后,将回车键生成的换行符留在了输入队列中。后面的cin.getline()看到换行符,将以为是个空行,并将一个空字符串赋给address)
解决方法(在读取之前 先读取并丢弃换行符)
#include "stdafx.h"
#include<iostream>
#include<cmath>
#define ZERO 0
#include<climits>
using namespace std;
int main()
{
const int line = 80;
cout << "请输入你的房子哪年建造" << endl;
int year;
cin >> year;
cin.get();
cout << "请输入你房子的地址" << endl;
char address[line];
cin.getline(address, line);
cout << "在" << address << "的房子,建造于" << year << endl;;
}
3.4输入char 和 string 的不同
char charr[20];
string str;
cin.getline(charr, 20);
getline(cin, str)
//长度
strlen(charr);
str.size();
4.结构(可以同时存储int,long,double)
4.1结构数组
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
struct Infor
{
char name[20];
int age;
double price;
};
int main()
{
Infor guest[2] =
{
{"小米", 20, 50.5}, //用大括号 不是小括号
{"华为", 25, 60}
};
cout << "顾客名字:" << guest[0].name << "和" << guest[1].name
<< "\n 总价"
<< guest[0].price + guest[1].price <<endl;
return 0;
}
5.共用体(只能存储int, long,或double 类型)
struct widget
{
char brand[20];
int type;
union id
{
long id_num;
char id_char[20];
} id_val;
};
wight prize;
if(prize.type ==1)
cin >> prize.id_val.id_num;
else
cin >> prize.id_val.id_char;
//或者让共用体匿名,因此id_num和id_char 被视为prize的两个成员。他们地址相同,不需要中间标识符
id_val
struct widget
{
char brand[20];
int type;
union
{
long id_num;
char id_char[20];
};
};
wight prize;
if(prize.type ==1)
cin >> prize.id_num;
else
cin >> prize.id_char;
6.枚举(enum)
//设置枚举量的值
enum bits{first, second = 100, third}
//first 默认情况下为0, 没有初始化的枚举量的值将比其前面的枚举量大1,即third = 101
enum {zero, null = 0, one, two = 1};
每个枚举都有取值范围 可以将取值范围内的任何整数值赋给枚举变量