顿开教育学习笔记
Lambda表达式\{}123456789101112131415161718//求最大值的函数auto pfun=[](int a,int b)->int{ int sum = a + b;};//调用方式cout << pfun(2,3);//第二种调用方式cout << [](int a,int b)->int{ int sum = a + b;}(6,1);//最简单的lamba表达式[]{cout<<"最简单的lambda表达式"<<endl;}();//加()调用
lambda表达式用法:
1. 回调函数
2. [说明符]
1. = 按值传入外部变量 `不能修改` 在内部捕获之后,相当于const修饰 此时就可使用mutable修饰[]\(\)mutable{}
2. & 按引用导入外部变量
1. = 和 & 的区别
2.
...
顿开教育学习笔记
C++中使用正则表达式添加头文件 ==#include==
使用场景:
1. 检测登录账号是否符合格式
2. 提取读取信息中需要的部分
3. 变量名的匹配
1、 regex_match()函数123456789101112#include <iostream>#include <regex>using namespace std;int main(){ string str = "abbb"; regex re(".b*$"); bool result = regex_match(str , re); cout << boolalpha << result << endl; return 0;}
输出 true
2、 regex_search()函数1regex_search("sdasdasffgerdf",regex(".*"));
强制类型转换
1234567891011//强制转换类型static_cast<>();reinterpret_cast<>();//cast关键字const int ease = 81;const char str[20] ="dsjaid";cahr * ps = const_cast<char *>(str); //ps指针就可以修改str的值
父类指针转换为子类对象123456789101112131415void show(Animal *base){ base->cry(); Cat* pc = dynamic_cast<Dog *>(base); if(pc) { pc->catchMouse(); } Dog* pd = dynamic_cast<Cat *>(bash); if(pd) { pd->seeHome(); }}
Try Catch异常处理
Trty Catch
异常处理的一些操作和步骤
一个try可以对应对个catch
1234567891011f(int a){ if( a < 1 ) throw a;}try{ }catch(int){ cout << "a小于1";}
可以单独写一个类处理和接受异常
12345678910111213141516171819202122232425262728293031323334#include <iostream>using namespace std;//异常类class listException{pubilc: listException(string str):str(str){} void print() { cout << str <<endl; }protected: string str ...
设计实例(顿开)
12345678910111213141516171819202122232425262728293031323334353637#include <iostream>#include <map>using namespace std;class Worker{public: Worker(string name,int salary,int age,string tel):_name(name),_tel(tel),_age(age),_salary(salary){} void show() { cout << _name << '\t' << _age << '\t' << _tel << '\t' << _salary <<endl; }private: string _name; int _ ...
顿开教育学习笔记
一、多态多态成立三要素
要有继承
虚函数重写
要有父类指针指向子类对象
多态是框架的基础
顿开教育学习笔记
template123456//重载<<(左移)运算符friend ostream& operate<<(ostream const& out,people worker){ out << worker.name << worker.age; return out;}
类模板有一处重点,涉及类的构造函数,构造函数时类模板会识别定义的结构体
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122#include <iostream ...
顿开教育学习笔记
C++IO流
通过#include 提供的控制符,进行输入输出格式控制
1234567891011121314151617181920212223242526272829#include <iostream>#include <iomanip>#include <string>using namespace std;int main(){ cout << "1" << endl; cerr << "标准错误" << endl; clog << "标准信息" << endl; cout.put('a'); cout.put('\n'); //不够不空格,多了就截取 cout.write("LOVEYOU\n", 10); // 10可现实元素个数 bool b = 1.1; cout << boolalpha << b &l ...
迭代(顿开)
迭代遍历12345678910111213141516171819//数组int arr[]{1,2,3,4,5,6};cout << "size(arr)" << size(arr) << endl; //6cout << "sizeof(arr[0])" << sizeof(arr[0]) << endl; //4cout << "sizeof(arr)" << sizeof(arr) << endl; //24//新用法——迭代cout << "arr遍历:" << endl;for(int i : arr) { cout << i << "\t";}//简单用法autofor(auto i : arr) { cout << i << " ...
内联函数(顿开)
一、内联函数123456//内联函数 //inline fun(){ }
二、默认参数和占位参数
默认参数:必须从右往左写(一旦出现默认参数,后边的参数必须都是默认的)
12345678//占位参数 /* 1.占位但是不调用 2.为之后的函数扩充留下线索 */void print(int){ cout << "PRINT()";}
三、函数重载1234567int fun(int ,int){ }int fun(char , char){ }
四、string12345678910string name = "mingming";for(auto i : name){ cout << i ; // mingming}//转c风格字符串const char* cname = name.data();