一 类型转换
c 方式强制类型转换存在的问题
1. 过于粗暴 任意类型之间都能转换,编译器很难判断其正确性 2. 难于定位 在源码中无法快速定位所有使用强制类型转换的语句
1.1 static_cast强制类型转换
1.用于基本类型间的转换,但是不能用于基本类型指针之间的转换 2.用于有继承关系类对象之间的转换和类指针之间的转换。 3.static_cast是在编译期间转换的,无法在运行时检测类型,所以类型之间转换可能存在风险。
#include <iostream> using namespace std; class Parent { }; class Child :public Parent { }; int main(void) { int a = 1; char ch = 'x'; a = static_cast<int>(ch); //用于普通类型之间的转换 //int *p = static_cast<int *>(&ch); Parent p; Child c; //p = c; p = static_cast<Parent>(c); //用于有继承关系的类对象之间的转换 //c = static_cast<Child>(p); Parent *p1 = static_cast<Parent *>(&c); //类对象指针之间的转换 }
1.2 reinterpret_cast
#include <iostream> using namespace std; int main(void) { int c = 200; int b = 100; char *p = reinterpret_cast<char *>(&c); //用于普通指针之间的转换(不安全) cout << *(p + 1) << endl; //cout << *(&b + 1) << endl; int *q = reinterpret_cast<int *>(100); //用于数字和指针之间的转换(很容易出现野指针) *q = 1; return 0; }
1.3 const_cast
#include <iostream> using namespace std; int main(void) { const int a = 1; //常量 int *p = const_cast<int *>(&a); *p = 100; cout << a << endl; cout << *p << endl; const int &m = 1; int &n = const_cast<int &>(m); n = 100; cout << m << endl; cout << n << endl; const int x = 1; int &y = const_cast<int &>(x); y = 100; cout << x << endl; cout << y << endl; int z = 200; const int *p1= &z; int *q = const_cast<int *>(p1); *q = 300; cout << *q << endl; cout << *p1 << endl; return 0; }
1.4 dynamic_cast
#include <iostream> using namespace std; class Parent { public: virtual void f() { } }; class Child :public Parent { public: void f() { } }; int main(void) { Child *c = new Child; delete c; //c = static_cast<Child *>(new Parent); //派生类指针指向基类对象(错误) c = dynamic_cast<Child *>(new Parent); //运行时候会进行类型检查,不能转换,会返回NULL,比static_cast安全 if (NULL == c) { cout << "转换失败" << endl; } else { cout << "转换成功" << endl; delete c; } return 0; }
二 算法
更易型算法
非更易型算法
排序算法
#include <algorithm> #<numeric> //数学运算的模板函数 #<functional> //提供了一些模板类,用来声明函数对象
2.1 遍历算法
#include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; class Print { public: void operator()(int x) { cout << x << endl; } }; void show(int x) { cout << x << endl; } int main(void) { int array[5] = { 1,2,3,4,5 }; vector<int> v(array,array + 5); //for_each //for_each(v.begin(), v.end(), show); //回调函数 for_each(v.begin(), v.end(), Print()); //函数对象的形式遍历 //transform遍历过程中可以修改数据 string s("hello world"); transform(s.begin(),s.end(),s.begin(),::toupper); cout << s << endl; return 0; }
2.2 查找算法
#include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; bool GreaterTwo(int x) { return x > 2; } class Print { public: void operator()(int x) { cout << x << endl; } }; class GreaterThree { public: bool operator()(int x) { return x > 3; } }; void show(int x) { cout << x << endl; } int main(void) { int array[7] = { 6,2,4,5,2,5 }; vector<int> v(array, array + 6); auto it = adjacent_find(v.begin(), v.end()); if (it == v.end()) { cout << "不存在重复且相邻的元素" << endl; } else { cout << *it << endl; } bool ret = binary_search(v.begin(), v.end(), 4); //查找有序的序列,二分查找法 // [1,3) 3 [4,7) if (ret) { cout << "元素存在" << endl; } else { cout << "元素不存在" << endl; } int num = count(v.begin(), v.end(), 2); cout << num << endl; num = count_if(v.begin(), v.end(), GreaterTwo); //一元谓词 回调函数 cout << num << endl; it = find(v.begin(), v.end(), 3); if (it == v.end()) { cout << "元素不存在" << endl; } else { cout << *it << endl; } it = find_if(v.begin(), v.end(), GreaterThree()); if (it == v.end()) { cout << "元素不存在" << endl; } else { cout << *it << endl; } return 0; }
2.3 排序算法
#include <iostream> #include <algorithm> #include <vector> #include <string> #include <time.h> using namespace std; bool GreaterTwo(int x) { return x > 2; } class Print { public: void operator()(int x) { cout << x << endl; } }; class GreaterThree { public: bool operator()(int x) { return x > 3; } }; void show(int x) { cout << x << endl; } int main(void) { vector<int> v1; vector<int> v2; for (int i = 0; i < 10; i++) { v1.emplace_back(rand() % 10); v2.emplace_back(rand() % 10); } sort(v1.begin(), v1.end(), less<int>()); stable_sort(v2.begin(), v2.end(), less<int>()); for (auto &v : v1) { cout << v << " "; } cout << endl; for (auto &v : v2) { cout << v << " "; } cout << endl; vector<int> v3; v3.resize(20); merge(v1.begin(),v1.end(),v2.begin(),v2.end(),v3.begin()); for (auto &v : v3) { cout << v << " "; } cout << endl; random_shuffle(v1.begin(), v1.end()); for (auto &v : v1) { cout << v << " "; } cout << endl; reverse(v3.begin(), v3.end()); for (auto &v : v3) { cout << v << " "; } cout << endl; string s("hello world"); reverse(s.begin(), s.end()); cout << s << endl; return 0; }
2.4 拷贝替换
#include <iostream> #include <algorithm> #include <vector> #include <string> #include <time.h> using namespace std; void show(int x) { cout << x << endl; } int main(void) { vector<int> v1(5, 1); vector<int> v2(5, 2); v2.resize(6); copy(v1.begin(),v1.end(),++(v2.begin())); for_each(v2.begin(), v2.end(), show); cout << endl; swap(v1, v2); for_each(v2.begin(), v2.end(), show); cout << endl; return 0; }
三 设计模式
创建型模式: 工厂模式,抽象工厂模式,单例模式,建造者模式等 结构型模式: 关注类和对象的组合,继承被用来组合接口和定义组合对象 适配器模式,桥接模式 组合模式,外观模式 等 行为型模式: 关注对象之间的通信
3.1 设计原则
1.开放封闭原则:所有新增的功能不是通过类的改动来实现,而是通过新增代码来实现。 2.依赖倒置原则:依赖与抽象,不依赖于具体的实现
#include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; class BankWoker { public: virtual void Worker() = 0; }; class GetMoney :public BankWoker { public: void Worker() { cout << "取款业务" << endl; } }; class SaveMoney :public BankWoker { public: void Worker() { cout << "存款业务" << endl; } }; int main(void) { BankWoker *b = new GetMoney; b->Worker(); delete b; b = new SaveMoney; b->Worker(); return 0; }
#include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; class HardDisk { public: virtual void work() = 0; }; class CPU { public: virtual void work() = 0; }; class AHardDisk :public HardDisk { public: void work() { cout << "hard disk work..." << endl; } }; class ACPU:public CPU { public: virtual void work() { cout << "ACPU WORK..." << endl; } }; class Computer { private: HardDisk *m_h; CPU *m_c; public: Computer(HardDisk *h, CPU *c) { m_h = h; m_c = c; } void work() { m_h->work(); m_c->work(); } }; int main(void) { CPU *c = new ACPU; HardDisk *h = new AHardDisk; Computer com(h,c); com.work(); return 0; }