04/29/2020
C++练习题
声明和定义-分离式编译机制
变量的定义的基本形式:类型说明符+变量名列表,以分号结束,可以赋初始化。任何包含了显式初始化的声明即成为定义。
- 枚举类型的前置声明1 :枚举类型使我们可以将一组整型常量组织在一起。枚举属于字面值常量类型。
int sum = 0, value; //sum是定义,value声明并定义,算定义。
extern double point; //声明
extern double length = 100; //定义,因为显式初始化了,如果在函数中,会报错。
class Book; //类声明
class Book{}; //类定义
Book bookPtr; //定义
struct Note; //结构体声明
std::string book("Gaming"); //定义
book = "None"; //不是声明也不是定义,赋值
int print(double); //函数声明
int print(double a){std::cout<< a; retrun 1;} //函数定义
int ok = print(3.4); //定义
enum Card2; //可以在全局作用域中使用的声明,默认为int类型
enum Card:int; //前置声明必须指定其成员的大小
enum Card {Ace,Jack,Quene,King}; //定义
enum class Book2; //声明默认成员类型是int,限定作用域声明
union Name; //声明
union Name{}; //定义
int arr[3]; //定义
int arr3[3]{1,2,3}; //定义
vector<int> a; //定义
extern vector<int> a; //声明
auto error; //错误的,auto必须有初始值用来推断类型。
auto error2 = 3; //定义
typedef std::vector<int> data; //定义
using wages = double; //定义
namespace std {int i =3;}; //定义
template<typename T, class D> T abc(D b){return T;} //定义
template<typename T,class D> T abc(D); //声明
sizeof类型大小的推算2
sizeof运算符返回一条表达式或一个类型名字所占的字节数。
- 对char类型得1
- 对引用类型是被引用对象所占空间的大小
- 对指针执行,得到指针本身所占空间的大小
- 对解引用指针执行,得到指针指向的对象所占空间的大小,忽略指针无效性
- 对数组执行,等价于每个数组所有元素各执行一次并所得到结果求和
- 对vector或string对象执行,得到一个固定部分的大小
sizeof运算符的特点:
- 当sizeof数组的时候,数组不会自动转换为指针类型
//内置类型
char c; //1
string s; //28
const int MAX{2324}; //4
extern double d; //8
//指针或数组
char* msgs[]{"HelloWorld!"}; //12 是数组包含一个空字符。
const char* msg{"HelloWorld!"}; //4 指针类型
const char* Director[]{"up","down"}; //8 有两个元素,每个元素是指针类型的 4+4
int arr[3]; //12 3个int
//类型别名
typedef vector<double> data; //16 vector类型是16
using A = double; //8
//函数
double length(double); //函数声明不可以
double length(double){return 4;} //函数定义也不行
//结构体或类
struct Node; //错误的,结构体声明并没有确定的内存空间
struct Node{int a, double b;} //16, double是8,对齐原理
//枚举类型
enum Season:long long; //8
enum Joker{Sun,Moon}; //4 默认是int类型
//命名空间
namespace A{int a;} //错误的,命名不可以
别名声明(alias declaration)
别名声明来定义类型的别名,传统用typedef,新出来可以用using。也叫类型别名(type alias)3。
- 定义
- 类型的同义词
内置别名
using unsigned_char = unsigned char;
using const_unsinged_char = const unsigned char;
using pointer_to_int = int*; //指向int的指针
using pointer_to_pointer_int = int**; //指向int*的指针
using pointer_to_array_with_element_type_char = char*; //指向一个数组,它的元素类型是char
using array_with_10_pointer_to_int = ptr_to_int[10]; //有10个元素是int类型的数组
using pointer_to_array_with_10_pointer_to_int = array_with_10_pointer_to_int*; //有10个元素是int的数组的指针
using array_20_of_array_10_with_pointer_to_int = ptr_to_int[20][10]; //有20个元素是有10个元素是int的数组的数组
//typedef
typedef unsigned char unsigned_char;
typedef int* ptr_to_int;
typedef ptr_to_int arr_of_10_ptr_to_int[10];
函数别名
void f(char*, int&); //声明一个函数
using PF = void(*)(char*,int&); //声明一个函数指针
using F1 = void(PF); //声明一个函数,它的参数列表有一个函数指针
using F2 = PF(); //申请一个函数指针的返回值
函数声明与参数列表的传递
- 参数列表可以省略形参的名字,因为不需要函数体。
- 函数指针或非函数指针做参数列表
- 如何验证它们,可以把声明写main函数前面,定义写在main函数后面
int a(int i); //普通函数申明
int a(int); //隐藏了变量名
int a(int(i)); //多余的小括号,但不算错
int a(int(*i)()); //函数指针
int a(int (*)()); //函数指针
int a(int i()); //隐式的自动转换成函数指针
int a(int ()); //隐式的函数指针但是没用名字
int main(){}
//definition of above function
函数的引用
using x = int(&)(int,int); //x是一个函数的引用
int fun(int,int);
int main()
{
x y{fun}; //int (&x)(int,int) = func;或者 int (&x)(int,int){fun};
std::cout << y(1,2);
}
int fun(int a, int b)
{
return a * b;
}
运算符的优先级和结合律4
- 作用域
- 成员选择
- 下标
- 函数调用
- 类型构造
- …
- 复合赋值
- 抛出异常
- 逗号
a = b + c * d << 2 & 8; // * + << & = (((b+(c*d))<<2)&8) 都是左结合律
a && 077 != 3; //!= && a&&(077!=3) 都是左结合律
a == b || a == c && c < 5 //< == && || (a == b)|| ((a==c) && (c<5)) 都是左结合律
0 <= i < 7; // <= < 一样的优先级 (0<=i)<7 都是左结合
a = -1+ +b---5; //后置递减 (一元+号 一元-号) (+ - 号) = a = (((-1) + (+(b--)))-5);
*p++; //后置递减 解引用 *(p++)
*--p; //(前置递减 解引用)同一优先级 *(--p)
++a--; //错误的
*p.m; //成员选择 解引用 *(p.m)
前置与后置运算符运用
unsigned u =0;
bool b = false;
double d = 0.0;
cout << endl;
cout <<"u-- = " << u-- <<endl; //0
cout <<"u = " << u <<endl; //-1 但是unsigned取模4294967295
cout <<"++u = " << ++u <<endl; //0
cout <<"u = " << u <<endl; //0
cout << endl << boolalpha; //输出bool类型用true和false代替
cout << "b = "<< b<<endl; //false
cout << "b++ = "<< b++ <<endl; //false
cout << "b = "<< b<<endl; //true
cout << "b++ = "<< b++ <<endl; //true
cout << "b = "<< b<<endl; //true 不会是2,还是1
//bool 类型无法使用递减
b = false;
cout << "++b = "<< ++b <<endl; //true
/*cout << "--b = "<< --b <<endl;*/ //错误的
/*cout << "b-- = "<< b-- <<endl;*/ //错误的