右值引用
#includeusing namespace std; class A { public: A() { cout<<"A的构造"< cout<<"A的析构"< cout<<"A的拷贝构造"< cout<<"AAAAAAAAAA"< public: B() { cout<<"B的构造"< cout<<"B的析构"< cout<<"BBBBBBBBB"< // A* a = new B(); // a->show(); // A a = B(); //A的拷贝构造,C++中这种在栈区直接用构造函数的形式叫做临时对象。 // //把B中属于a的部分赋值给a 这种情况是两个不同空间的对象间的拷贝复制,a中的指针并不会操作另一个对象的虚表 // //对象a只是一个A的通过拷贝构造得到的 // const A& a2 = B(); 常引用 // //在C++中临时对象,也是一种形式常对象,必须用const引用 // cout << "Hello World!" << endl; // return 0; A&& a2 = B(); // 右值引用,可以引用临时变量,也可以引用常量,不能引用左值 a2.show(); return 0; }
函数模板
#includeusing namespace std; template T My_add(T t1, T t2) { return t1 + t2; } //函数模板重新翻译一遍成为正真的函数, T数据类型的占位符 int main() { cout< (1, 5)< 告诉编译器使用模板 cout << "Hello World!" << endl; cout< ("1.0my", "5.0head")< 类模板
#includeusing namespace std; template class A { T1 t1; T2 t2; T3 t3; public: T1 show(T1 t1, T2 t2) { cout< class B : public A //使用继承关系是模板类的父类的声明格式 { public: void showInfo(T1 t1, T4 t4) { cout< A a; //类模板必须指定类型 a.show(1, 3.14f); cout << "Hello World!" << endl; B b; b.showInfo(1, 3.14); return 0; } 类模板分文件编程
#ifndef MY_STACK_H #define MY_STACK_H // 防止重复包含 #includeusing namespace std; template class MyStack { private: int capacity; int size; T* data; public: MyStack(int c=10); ~MyStack(); bool push(T t); bool pop(); T top(); bool empty(); }; template MyStack ::MyStack(int c):size(0), capacity(10) { data = new T[c]; } template MyStack ::~MyStack() { delete []data; } template bool MyStack ::push(T t) { if(size>capacity) return false; data[size] = t; size++; return true; } template bool MyStack ::pop() { if(size == 0) return false; size--; } template T MyStack ::top() { if(size>0) { T temp = data[size-1]; return temp; } } template bool MyStack ::empty() { if(size==0) return true; return false; } #endif // MY_STACK_H //模板文件与声明在同一个文件中 #include#include using namespace std; int main() { MyStack stack; //在编译的时候就需要定义完模板类 stack.push(1); stack.push(2); stack.push(3); for(int i=0;!stack.empty();i++) { cout< auto_ptr智能指针
#include#include //智能指针,动态内存管理 using namespace std; class A { int Id; public: A(int id):Id(id) { cout<<"A的构造函数"< cout<<"A的析构函数"< cout< Id< // A* p1 = new A(888); // p1->showInfo(); // delete p1; auto_ptr p2(new A(888)); p2.get()->showInfo(); //get获得类对象中的指针 auto_ptr p3(p2); p3.get()->showInfo(); p2.get()->showInfo();//p2已被释放,空指针指向showInfo return 0; } 智能指针-类模板
#include#include using namespace std; class A { int Id; public: A(int id):Id(id) { cout<<"A的构造函数"< cout<<"A的析构函数"< cout< Id< class AutoPtr { private: T* p; public: AutoPtr(T* _p): p(_p) { } ~AutoPtr() { delete p; } T* get() { return this->p; } AutoPtr(AutoPtr& autoptr) { this->p = autoptr.p; autoptr.p = nullptr; } }; int main() { // AutoPtr p(new A(10)); // p.get()->showInfo(); // AutoPtr p2(p); // p2.get()->showInfo(); // cout << "Hello World!" << endl; shared_ptr ptr(new A(20)); ptr.get()->showInfo(); shared_ptr ptr2(ptr); ptr2.get()->showInfo(); ptr.get()->showInfo(); return 0; } shared_ptr智能指针
#includeusing namespace std; template class RefCount { private: T* p; int m_count; public: RefCount(T* ptr=nullptr): p(ptr) //将p初始化为ptr { if(p!=nullptr) { m_count = 1; } cout<<"ReCount计数器的构造"< cout<<"RefCount计数器的析构"< return this->m_count++; } int delRef() { return --m_count; } int getCount() { return m_count; } }; template class SharedPtr { private: T* p; RefCount * refcnt; public: SharedPtr(T* ptr = nullptr): p(ptr) { refcnt = new RefCount (p); } SharedPtr(const SharedPtr& src): p(src.p), refcnt(src.refcnt) //拷贝构造 { if(p!=nullptr) { refcnt->addRef(); } } SharedPtr& operator=(const SharedPtr& src) { this->p = src.p; this->refcnt = src.refcnt; refcnt->addRef(); return *this } ~SharedPtr() { if(refcnt->delRef()==0) { delete p; delete refcnt; p = nullptr; refcnt = nullptr; } } T* get() { return this->p; } T* operator->() { return this->p; } int use_count() { return refcnt->getCount(); } }; int main() { cout << "Hello World!" << endl; return 0; } //智能指针:类模板,用于动态分配内存,将基本类型指针封装为类对象 //指针,离开作用域时调用析构函数, // 作用:能够处理内存泄漏问题和空悬指针问题 weak_ptr智能指针
#include#include using namespace std; class B; // 前置声明 class A { public: weak_ptr ptr_b; //智能指针对象 public: A() { cout<<"A的构造"< cout<<"A的析构"< cout<<"hello_world"< public: shared_ptr ptr_a; public: B() { cout<<"B的构造"< cout<<"B的析构"< } }; int main() { shared_ptr pA(new A); shared_ptr pB(new B); pA->ptr_b = pB;// 赋值计数器+1 交叉引用目的:ptr_b调用a中的函数。 pB->ptr_a = pA; cout << "Hello World!" << endl; return 0; } 异常处理
#includeusing namespace std; class Error { private: string errorMsg; public: Error(string errorMsg) { this->errorMsg = errorMsg; } string what() { return this->errorMsg; } }; Error err("被除数不能为0"); float my_div(int a, int b) { if(b==0) throw err; return a / b; } int my_add(int a, int b) { return my_div(a, b)+a+b; } int my_mul(int a, int b) { return my_add(a, b)*a*b; } int main() { int my_data; int a=10; int b=0; try{ my_data = my_mul(a, b); cout< cout< 容器vector
#include#include // 动态数组,不需要指定长度 #include using namespace std; template bool cmp(const T& a, const T& b) { return a>b; } void test() { vector _vector; // 对STL容器vector插入数据 _vector.push_back(1); _vector.push_back(5); _vector.push_back(3); _vector.push_back(8); _vector.push_back(2); // 对STL容器vector进行遍历;迭代器 vector ::iterator it; for(it = _vector.begin();it!=_vector.end();it++) { cout<< *it < ); for(it = _vector.begin();it!=_vector.end();it++) { cout<< *it < test(); return 0; } vector
#include#include using namespace std; void test() { string str("hello world"); cout< cout< test(); cout << "Hello World!" << endl; return 0; }c++算法库
#include#include #include using namespace std; void show(int& value) { cout< public: void operator()(int& value) { cout< // vector _vector; // for(int i=0;i<30;i++) // { // _vector.push_back(i); // cout<<"_vector容量:"<<_vector.capacity()<<"t 实际用了:"<<_vector.size()< 1,3,5}; // vector _vector(arr, arr+3); //定义vector对象 // cout<<_vector[2]< _vector(arr, arr + sizeof(arr) / sizeof(int)); // 1、枚举for循环 for(int k : _vector) { cout< cout<< *it < 返回值{//匿名函数体} []:标识函数体内所要捕获外部变量方式 // ()匿名函数的参数列表。->返回值 for_each(_vector.begin(), _vector.end(), [&](int& value){ cout< test(); cout << "Hello World!" << endl; return 0; } 双端队列
#include#include //双端队列 #include using namespace std; void show(int& value) { cout< deque _deque; _deque.push_back(10); _deque.push_back(20); _deque.push_back(30); _deque.push_back(40); _deque.push_back(50); for_each(_deque.begin(), _deque.end(), &show); sort(_deque.begin(), _deque.end(), less ()); for_each(_deque.begin(), _deque.end(), &show); } int main() { test(); cout << "Hello World!" << endl; return 0; } // 允许收尾两端快速插入及删除;无迭代器 双端列表
#include#include //双向链表 #include using namespace std; void test() { list
_list; _list.push_back(10); _list.push_back(20); _list.push_back(30); _list.push_back(80); _list.push_back(60); _list.push_back(50); _list.push_back(40); _list.sort(); for_each(_list.begin(), _list.end(), [&](int& value){ cout< test(); cout << "Hello World!" << endl; return 0; } 容器适配器
#include#include #include using namespace std; void test() { stack _stack; _stack.push(55); _stack.push(13); _stack.push(3); _stack.push(23); cout<<_stack.top()< cout<<_stack.top()< queue _qu; _qu.push(10); _qu.push(20); _qu.push(30); while(!_qu.empty()) { cout<<_qu.front()< test(); cout << "Hello World!" << endl; return 0; } // 容器适配器 set
#include#include #include using namespace std; void show(int value) { cout< set _set1; _set1.insert(13); _set1.insert(33); _set1.insert(23); _set1.insert(43); _set1.insert(53); _set1.insert(15); for_each(_set1.begin(), _set1.end(), &show); auto it = _set1.find(53); if(it != _set1.end()) { cout<< *it < test(); cout << "Hello World!" << endl; return 0; } c++算法
#include#include #include using namespace std; class Stu { private: int id; string name; int age; public: Stu(int id=1000, string name="", int age=0) { this->id = id; this->name = name; this->age = age; } void showInfo() const { cout<<"学生信息, 学号:"< id<<"tX姓名:"< name<<"t年龄:"< age< friend class My_compair; }; template class My_compair { public: // 返回值为bool 谓词,二元谓词 bool operator()(T t1, T t2) { return t1.id // set中的比较器,只能用函数对象,不能使用函数指针 // My_compair 仿函数 set > _s; //My_compair 仿函数 _s.insert(Stu(1000, "zhangsan", 20)); _s.insert(Stu(1001, "lisi", 19)); _s.insert(Stu(1002, "wanwu", 21)); _s.insert(Stu(1003, "zhaolu", 22)); _s.insert(Stu(1004, "tianqi", 23)); std::pair >::iterator, bool> p; p = _s.insert(Stu(1005, "songba", 24)); if(!p.second) { p.first->showInfo(); } for_each(_s.begin(), _s.end(), [&](const Stu& stu){ //常对象 stu.showInfo(); // 常函数 }); std::pair p1 = std::make_pair(1005, "zhangsan"); //制作一个数组 } int main() { test(); cout << "Hello World!" << endl; return 0; } //迭代器取* 对象 map
#include#include 文件操作
#include#include #include using namespace std; void write() { ofstream file; //定义写文件的对象 file.open("C:\Users\17931\Desktop\qt.txt", ios_base::app); ofstream file2("./2.txt", ios_base::app); //构造函数 if(!file.is_open()) { cout<<"打开失败"< ifstream file; //定义一个度文件对象 file.open("C:\Users\17931\Desktop\qt.txt", ios_base::in); if(!file.is_open()) { cout<<"打开失败"< cout< ifstream file; file.open("C:\Users\17931\Desktop\qt.txt", ios_base::app); if(!file.is_open()) { cout<<"打开失败"< cout << "Hello World!" << endl; return 0; } 线程
#include#include #include using namespace std; //函数指针 void show() { cout<<"线程开始执行"< cout<< i < public: void operator()() { cout<<"线程开始执行"< cout<< i < cout<<"线程开始执行"< cout<< i < //1、使用全局函数指针来实现线程入口 thread my_thread(&show); my_thread.join(); //2、使用匿名函数Lambda表达式来实现 thread my_thread2([](){ cout<<"线程开始执行"< cout<< i <



