C++尝鲜 1 2 3 4 5 6 7 8 9 #include <iostream> using namespace std;int main () { int num = 0 ; cout << "how old are you ?" << endl; cin >> num; cout << "you are" << num << " years old" << endl; return 0 ; }
1 2 3 4 5 6 7 8 #include <iostream> std::cout std::cout << 变量; std::cout << 变量 << string << endl; std::cout << 变量 << std::endl; std::cin std::cin >> 变量
C++基础 声明和定义变量
多个文件使用同一个变量,声明和定义必须分离,且变量的定义只能出现在一个文件中,其他使用到该变量的文件则必须对其声明,不能重复定义
头文件 climits定义了基本数据类型的最大最小值符号常量1 2 3 4 5 int x;const int month = 12 ; extern int i; int j = 3.14 ; extern int i = 0 ;
auto关键字 auto根据初始化值的类型推断变量的类型1 2 3 auto n = 100 ;auto x = 1.5 ;auto str = "kyeleson"
引用(左值引用& 右值引用&&) 1 2 3 int val = 1024 ; int &ref_val = ival;int i = ref_val; i 被初始化为val的值
ref_val指向val 引用即别名,并非引用对象,仅仅为所存在的对象声明一个新名字 引用的初始值必须是一个对象,只能被初始化一次,不可修改 一个变量可以定义多个引用 对引用的操作与对变量的直接操作完全一样,即修改引用ref_val等同于修改val
1 2 3 4 5 6 7 8 9 10 #include <iostream> int main () { int val = 10 ; int &ref_val = val; ref_val = 32 ; std::cout << val << std::endl; getchar (); return 0 ; } 结果:32
右值引用->临时对象,绑定到右值,可修改且不会再被利用 利用廉价的移动操作代替昂贵的拷贝操作以此优化性能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 string &&str ("hello" ) ;template <class T >string swap (T &a ,T &b) { T temp = a; a = b; b = temp; }template <class T >string swap (T &a ,T &b) { T temp = move (a); a = move (b); b = move (temp); }
数组 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 int array[5 ];int array[] = { 1 ,2 ,3 ,4 };double array[4 ]{ 1.2 ,3.4 ,3.2 ,4.5 }; char array[] = { 'a' ,'b' ,'c' ,'\0' };char array[2 ] = { 'a' ,'b' ,'\0' };char array[2 ] ={'A' ,'b' }; char str[] = "hello" ;int zippo[4 ][2 ];int array[][];int array[][4 ];
字符串 TODO:字符串操作 (字符串处理:直到遇到’\0’才结束)
1 2 3 4 5 6 7 8 9 10 11 12 char array[] = { 'a' ,'b' ,'c' ,'\0' };char array[2 ] = { 'a' ,'b' ,'\0' };char array[2 ] ={'A' ,'b' }; char str[] = "hello" ; 、、string类#include <string> string str = "hello" ; str = "world" ; string str = {"hello world" }; string str{"hello world" };string c (5 , 'A' ) ;
1 2 char dog[8 ] = {'b' ,'e' ,'a' ,'u' ,'x' ,'',' T',' T'}; // not a string char cat[8] = {' f',' a',' t',' a',' s',' s',' a',' \0 '}; // a string
‘A’:字符常量 “A”:字符串常量,字符’A’+’\0’
C风格字符串的比较需要用到strcmp()函数,字符串变量本身代表的是地址,不能直接使用“=”比较 1 2 3 4 5 #include <cstring> char * first_name = "mate" ;char * last_name = "mate" ;int code = strcmp (first_name,last_name);printf ("code:%d" ,code);
结构体 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 struct person { char name[20 ]; int year; char sex; }; person p = {"kyleson" ,19 ,'男' }; std::cout << p1. name << std::endl; person *pp = &p;struct sample { int a; int b; int add () {return a + b;} }; sample s = { 1 ,2 }; std::cout << s.add () << std::endl;
每个struct只能有唯一的定义,对于两个struct,即使成员相同其本身仍是不同的类型
1 2 3 4 5 6 struct addr { int num; }; addr x; addr y = x;int i = x;
联合体 union union是特殊的struct,其所有成员都分配在同一地址空间,即在相同的内存位置存储不同的数据类型,union实际占用的空间大小与其最大的成员相同,并且在同一时刻union只保存一个成员的值
1 2 3 4 5 6 union value { char sex; int sex_num; } value val; val.sex = '男' ;
枚举 enum Name{…}枚举值隐式转换为整数类型,枚举值名字与枚举本身位于同一作用域1 2 3 4 5 6 7 8 9 10 11 enum color { red,blue,black,white }; color brand = red; std::cout << brand << std::endl; brand = 10 ; brand = color (blue); brand = color (4 );int c = red;enum bit {one = 10 ,two = 22 ,three}; bit b = three;
enum class Name{…}限定作用域的强类型枚举,枚举值不会隐式转换为其他类型,枚举值名字位于枚举局部作用域
1 2 3 4 enum class Color {red,green,yellow}; Color c = Color::red; Color color = red;int c = Color::red;
指定枚举初始类型1 enum class color :char {red,green,yellow};
指针 TODO:共享指针,智能指针 (指向对象的对象[存储指向对象的地址],在生命周期内可指向不同的对象)
1 2 3 4 5 6 int *p; int val = 19 ;int *ip = &val;
指针的值(状态):指向一个对象、指向紧邻对象所占空间的下一个位置、空指针、无效指针
利用指针访问对象:操作符 *
1 2 3 4 int num = 12 ; int * p = # std::cout << *p <<std::endl; 打印结果:12
空指针:不指向任何对象
1 2 int *p = nullptr ;int *p =0 ;
通过new分配内存
1 2 3 4 int *p = new int ; *p = 10 ; std::cout << *p << std::endl;delete p;
void* 指针:存放任意对象的地址
1 2 3 4 double pi = 3.14 ,*pd = &pi ;void pv = π pv == pd ? --> true note:不能直接操作void *指针所指向的对象,对象类型未知
指针的指针:指向指针的指针,存储指针的本身的地址
1 2 3 int val = 1024 ;int *p = &val; int **pp = &p;
指向常量的指针,不允许被修改
1 2 3 4 const double pi = 3.14 ;const double *p1 = πdouble *p2 = π *p1 = 42 ;
const指针:常量指针,指针本身也是常量,必须被初始化
1 2 3 4 5 6 7 8 int errNum = 0 ;int *const curErr = &errNum; const double pi = 3.14 ;const double *const pip = &pi ;int num = 1 ; curErr = # *curErr = 1 ; *pip = 1 ;
函数指针 指向函数的指针,保存着函数代码起始处的地址
1 2 3 4 5 6 7 8 函数:void toUpper (char *) ; 指向函数的指针:void (*pf) (char *); pf = toUpper; 函数指针赋值char mis[6 ]="hello" ; #把toUpper作用于mis (*pf)(mis);pf (mis)bool (*pf)(const string &,const string &)
指针和数组 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 int array[5 ] = {1 ,2 ,3 ,4 }; array本身是指向array第一个元素的指针 std::cout<< *array; ---> 1 char array[] = { 'a' ,'b' ,'c' ,'d' };char str1 = array[0 ];char str2 = *array;char str4 = array[1 ];char str3 = *(array +1 ); std::cout << str1 << str2 << str3 << str4 << std::endl; 输出:a a b bchar *start = begin (array); char *end = end (array); int *pts[10 ]; int (*ptr)[10 ]; char *name = "kyleson" ;char str[5 ] = "ABC" ;char *p = str;int array[] = { 1 ,2 ,3 };int *pa = array; std::cout << name << *name << *(name + 1 ) << std::endl; std::cout << p << *p << std::endl; std::cout << pa << *pa << std::endl; std::cout << string ----> (字符串处理:直到遇到'\0' 才结束)
类型别名 typedef 1 2 3 4 5 6 7 typedef double wages; typedef wages base,*p; wage hourly,weekly; typedef char * pstr; const pstr str = 0 ; const pstr* ps; const char * str ;
类型指示符 decltype 1 decltype (fun ()) sum = x ;
vector 1 2 3 4 5 6 #include <vector> vector<string> strs; vector<string> vetor = {"a" ,"person" ,"hi" };vector<int > v_num (10 ,9 ) ; vector<int > ten_num (10 ) ; strs.push_back ("hello" );
TODO:其他集合
存储持续性
自动存储持续性:函数定义中声明的变量/函数参数,其所属函数或代码块执行时被创建,执行完时被被自动释放; 静态存储持续性:函数定义外定义的变量或static修饰的变量,其在整个程序运行中都存在; 动态存储持续性:new分配的内存,直到使用delete释放为止;