【数据结构】二叉树的遍历
一、递归遍历 二叉树结构: 12345678typedef char Element;//二叉链式树节点typedef struct BtNode{ Element data; //数据域 struct BtNode* leftchild; //左孩子 struct BtNode* rightchild; //右孩子}BtNode, *BinaryTree; (1)先序12345678//前序遍历二叉树void PreOrder(BinaryTree root){ if (root == NULL) return; cout << root->data << " "; PreOrder(root->leftchild); PreOrder(root->rightchild);} (2)中序12345678//中序遍历二叉树void MidOrder(BinaryTree root){ if (root == NULL) return; MidOrder(root-&g ...
【数据结构】智能指针管理二叉树
二叉树结构12345678910111213141516171819202122232425262728#ifndef BINARY_TREE_H#define BINARY_TREE_H#include <iostream>#include<memory>using namespace std;typedef char Element;//二叉链式树节点typedef struct BtNode{ Element data; //数据域 struct BtNode* leftchild; //左孩子 struct BtNode* rightchild; //右孩子}BtNode, *BinaryTree;//二叉链式shared_ptr树节点#if 1typedef struct SBT_Node{ Element data; //数据域 std::shared_ptr<SBT_Node> leftchild; //左孩子 std::shared_ptr<SBT_Node> rightchil ...
【数据结构】二叉树的创建
二叉树的创建与遍历以下图的二叉树为例:Binary_Tree.h 1234567891011121314#ifndef BINARY_TREE_H#define BINARY_TREE_Htypedef char Element;//二叉链式树节点typedef struct BtNode{ Element data; //数据域 struct BtNode* leftchild; //左孩子 struct BtNode* rightchild; //右孩子}BtNode, *BinaryTree;#endif 创建方式一Bt.cpp123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869#include"Binary_Tree.h"#include <iostream>using namespace std;//前序遍历二叉树 ...
【C++】线程同步二(条件变量+互斥锁)
题目:使用三个线程依次输出ABC(1)介绍RAII包装类unique_lock (2)介绍条件变量温馨提示:linux下的条件变量 (3)代码实现123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101#include <iostream>#include <thread>#include <mutex>using namespace std;//实现三个线程输出ABC ...#if 1//互斥锁std::mutex mtx;//条件变量std::condition_variable cv;//某个唤醒条件int con = 0;//con = 0唤醒线程1打印A //con = 1唤醒线程2打印B //con = 3唤 ...
【C++】线程同步一
一、线程同步题两个线程同时对一个全局变量++操作,保证最后的结果正确 (1)原子操作123456789101112131415161718192021222324252627282930#include <iostream>#include <thread>#include <mutex>#include <atomic>using namespace std;//线程同步一:两个线程同时对一个全局变量++操作,保证最后的结果正确//原子全局变量atomic_int g_data = 0;void add(int id){ for (int i = 0; i < 5; ++i) { ++g_data; cout << id << " add : "<< g_data << endl; }}int main(){ thread tha(add, 1); thread thb(add, 2); //主进程等待两个子 ...
【C++】std::move()仿写
一、关于std::move() 原理:将一个左值强制类型转成一个纯右值 二、仿写std::move12345678910111213141516171819202122232425262728293031323334//去除引用特性template<class _Ty>struct my_remove_reference{ using type = _Ty;};template<class _Ty>struct my_remove_reference<_Ty&>{ using type = _Ty;};template<class _Ty>struct my_remove_reference<_Ty&&>{ using type = _Ty;};//重命名template<class _Ty>using my_remove_reference_t = typename my_remove_reference<_Ty> ...
【C++】类型转换
关于四种类型转换1. 使用场景(1)static_cast<新类型>(表达式) 适用于不去除常量性(const)和异变性(mutable)的场合; 就是普通的类型转换 12int a = 10;double b = static_cast<double>(a); (2)const_cast<新类型>(表达式) 适用于去除常性的转换;为了修改&a的数据 12const int a = 10;int* pa = const_cast<int*>(&a); (3)reinterpret_cast<新类型>(表达式) 适用于重新解释:修改指针+1的识别能力和*解引用能力 12int a = 10;char* b = reinterpret_cast<char*>(&a); (4)dynamic_cast<新类型>(表达式) 适用于多态中的转换:将派生类地址转换成基类地址 123456789101112131415161718class Base{public: vir ...
【C++】不同模板对象之间赋值
代码: 123456789101112131415161718192021222324252627282930#include <iostream>using namespace std;template<class _Ty>class object{public: _Ty _val;public: object(_Ty val = 0) : _val(val) {} template<class other> object& operator=(const object<other>& src) { if (this != (object<_Ty>*) (&src)) { this->_val = (_Ty)src._val; } return *this; }};int main(){ object<int> obja(10); object<double> objb ...
【C++】编程识别普通指针和智能指针
一、模板特化类普通指针和智能指针的最大区别就是,智能指针是一个对象类型,而普通指针是一个指针类型,通过这一点,我们便可以使用通用模板和特化模板类进行区分,从而达到目的。12345678910111213141516171819202122232425262728293031323334353637#include <iostream>#include <memory>using namespace std;//普通对象版本template<class T>struct mybool{ static const bool result = true;};//指针特化版本template<class T>struct mybool<T*>{ static const bool result = false;};//判断是普通指针还是智能指针template<class _Ty>void fun(_Ty& value){ if (mybool<_Ty> ...