摘要: ```cpp #include #include //forward_list 是c++11中引入的单项串列(singal linked list) //namespace std { //template > //class //} //特点: 不支持随机元素访问, 访问头部元素速度快 //"forward_list" 和自己手写的c-style signal linked list相比 //没 阅读全文
posted @ 2020-02-24 19:51 i0gan 阅读(213) 评论(0) 推荐(0) 编辑
摘要: ```cpp#include #include #include //list 是c++98中引入的双向串列(double linked list)//namespace std {//template>//class list;//}//特点: 不支持随机访问元素,访问头部和尾部元素快//任何位置插入删除元素都很快,常亮时间内完成//插入和删除不会造成迭代器的失效//对于异常支持好, 出现异常对... 阅读全文
posted @ 2020-02-24 19:47 i0gan 阅读(269) 评论(0) 推荐(0) 编辑
摘要: ```cpp #include #include //deque是c++98中引入的动态数组(dynamic array) //namespace std { //template> //class deque; //} //特点:随机访问元素, 末端和头部添加删除元素效率高,中间删除和添加元素效率低 //而vector仅仅操作尾部效率高 //元素的访问和迭代比vector要慢,迭代器不能是普通的 阅读全文
posted @ 2020-02-24 19:46 i0gan 阅读(216) 评论(0) 推荐(0) 编辑
摘要: ```cpp #include #include #include //#include //c++ 98 static void vectorPart() { //vector 是c++98中引入的动态数组(dynamic array) //namespace std { //template> //就是用默认的alloctor了, // class vector; //} //特点随机访问元素 阅读全文
posted @ 2020-02-24 19:44 i0gan 阅读(714) 评论(0) 推荐(0) 编辑
摘要: ```cpp #include #include #include //c++ 11 template void checkSize(C &c) { if(c.size() > 3) { c[3] = 10; //单线程OK,多线程可能出错 } c.at(3) = 10; } void arrayPart() { //array 实际上是对c/c++语言中的原生数组进行了封装 //namespac 阅读全文
posted @ 2020-02-24 19:43 i0gan 阅读(112) 评论(0) 推荐(0) 编辑
摘要: ```cpp#include #include #include #include #include #include #include #include #include #include #include #include //STL 是一个框架, 将数据结构和算法进一步的抽象//容器, 迭代器, 算法//容器: 储存东西的//类别://1 序列式容器 array / vector / deq... 阅读全文
posted @ 2020-02-24 19:41 i0gan 阅读(395) 评论(0) 推荐(0) 编辑
摘要: ```cpp #include #include #include #include //#include //#include //#include //#include //#include //#include using namespace std; /* keywords of c++ alignas(c++11) //声明结构体或类的对齐数 alignof(c++11) //查看结构体 阅读全文
posted @ 2020-02-24 19:30 i0gan 阅读(289) 评论(0) 推荐(0) 编辑
摘要: ```cpp#include #include #include #include #include static void autoValue();static void autoPointer();static void newVersionFor();static void newVersionConstruct();static void defaultInitValue();static... 阅读全文
posted @ 2020-02-24 19:27 i0gan 阅读(487) 评论(0) 推荐(0) 编辑
摘要: ```cpp #include #include //基础函数 void printInfo(int a, int b, int c) { std::cout void templatePrint(T1 a, T2 b, T3 c) { std::cout void operator() (T1 a, T2 b, T3 c) const { std::cout void printUseFun(F 阅读全文
posted @ 2020-02-24 19:23 i0gan 阅读(412) 评论(0) 推荐(0) 编辑
摘要: ```cpp #include #include #include //智能指针总结: /* */ void sharedPtrNotice(); class Parent; typedef std::shared_ptr ParentPtr; typedef std::weak_ptr WeakParentPtr; class Child : public std::enable_shared_ 阅读全文
posted @ 2020-02-24 19:20 i0gan 阅读(350) 评论(0) 推荐(0) 编辑
摘要: ```cpp #include #include #include class Object; typedef std::unique_ptr UniqueObjectPtr; using ObjectPtr = std::shared_ptr; void print(const UniqueObjectPtr& obj) {} class Object { public: Object(int 阅读全文
posted @ 2020-02-24 19:18 i0gan 阅读(178) 评论(0) 推荐(0) 编辑
摘要: #include <iostream> #include <memory> #include <cassert> /* //this //调用了两次析构函数, ParentPtr p(this); //智能指针出了作用域后就delete this ,析构了 //所以错了,为了解决这个问题,就需要自身 阅读全文
posted @ 2020-02-24 19:15 i0gan 阅读(721) 评论(0) 推荐(0) 编辑
摘要: ```cpp #include #include #include //auto ptr //shared_ptr //unique_ptr //weak_ptr class Parent; //采用前置声明 using ParentPtr = std::shared_ptr; typedef std::weak_ptr WeakParentPtr; class Child { public: / 阅读全文
posted @ 2020-02-24 19:11 i0gan 阅读(353) 评论(0) 推荐(0) 编辑
摘要: ```cpp #include #include static void interfaceOfSharedPtr(); int main(void) { interfaceOfSharedPtr(); return 0; } class Object { public: Object(int id) : m_id(id) { std::cout ; //作为参数的obj,相当于拷贝了一次,use 阅读全文
posted @ 2020-02-24 19:09 i0gan 阅读(145) 评论(0) 推荐(0) 编辑
摘要: ```cpp#include #include #include #include struct Base { virtual void f() { std::cout init(); return ret;}void testVirtual() { //Base b; //Derived d; ////虚函数调用通过引用 ////virtual funciton call through ref... 阅读全文
posted @ 2020-02-24 19:07 i0gan 阅读(636) 评论(0) 推荐(0) 编辑
摘要: ```cpp #include #include /* 类虚函数遇到构造和析构就退化了 */ class Event; //类的前置声明 class Event {}; class Base { public: virtual ~Base() {} //why? virtual Base(int _id) : m_id(_id) {} virtual void act(Event const&) 阅读全文
posted @ 2020-02-24 19:04 i0gan 阅读(180) 评论(0) 推荐(0) 编辑
摘要: ```cpp#include #include static void versionOne();static void versionTwo();using namespace std;int main(void) { versionOne(); versionTwo(); versionThree(); return EXIT_SUCCESS;}void versionOne() { //c语... 阅读全文
posted @ 2020-02-24 19:01 i0gan 阅读(264) 评论(0) 推荐(0) 编辑
摘要: ```cpp#include #include #include #include /* 建议: 在类的构造函数中抛出异常 */class A { public: A() {}};class B : public A { public: B() {} ~B() { /*std::cout m_info; //若作为状态判断,一般声明为私有函数, 通过assert来提示. bool i... 阅读全文
posted @ 2020-02-24 18:55 i0gan 阅读(266) 评论(0) 推荐(0) 编辑
摘要: ```cpp#include /* 不要在析构函数中抛出异常 */class A { public: A() {} ~A() { std::cout << "A析构了.." << std::endl; }};/* 析构函数调用规则: 先调用成员析构 再调用派生类析构 *///析构函数绝对不要抛出异常class B : public A{ public: B() {} ~B() { std... 阅读全文
posted @ 2020-02-24 18:52 i0gan 阅读(644) 评论(0) 推荐(0) 编辑
摘要: ```cpp #include #include #include #include #include class RuleOfFive; //前置声明class class Parent; class Child { public: explicit Child(Parent *p) : m_parent(p) {} private: Parent *m_parent; }; class Par 阅读全文
posted @ 2020-02-24 18:46 i0gan 阅读(584) 评论(0) 推荐(0) 编辑