随笔分类 -  C++

摘要:使用模板元编程进行递归编写,来实现编译期代码循环执行 例:给定一个无符号整数(unsigned int),求该整数对应的二进制数中有几个1 #include <iostream> template <size_t input> constexpr size_t onesCount = (input 阅读全文
posted @ 2024-02-19 22:37 Wangtn 阅读(15) 评论(0) 推荐(0) 编辑
摘要:构造: 现有父类后有子类 析构:和构造顺序相反,先析构子类后析构父类 #include <iostream> class A { public: A() { std::cout << "ctor father" << std::endl; } virtual ~A() { std::cout << 阅读全文
posted @ 2023-01-16 15:33 Wangtn 阅读(37) 评论(0) 推荐(0) 编辑
摘要:项目文件结构 cmake_minimum_required(VERSION 3.15) project(AddTest) message("CMAKE_CURRENT_BINARY_DIR: " ${CMAKE_CURRENT_BINARY_DIR}) message("CMAKE_CURRENT_ 阅读全文
posted @ 2023-01-04 10:47 Wangtn 阅读(350) 评论(0) 推荐(0) 编辑
摘要:目录结构 cmake将五个文件一起编译 在DataTransmission.h中,定义了单例模式的class SingletonDataTransfer 如果在头文件中初始化单例模式中的指针m_pInstance,或出现重定义问题,因为cmake会编译(DataTransmission.h Data 阅读全文
posted @ 2022-09-01 14:06 Wangtn 阅读(118) 评论(0) 推荐(0) 编辑
摘要:单例模式保证一个类仅有一个实例,并提供一个访问它的全局访问点 泛型单例模式需要变参构造函数,构造函数的参数个数需要支持变化 下面是不用变参模板,支持0~6个参数的单例模式实现 #include <iostream> // 泛型单例模式需要变参构造函数,构造函数的参数个数需要支持变化 // 支持0~6 阅读全文
posted @ 2022-08-29 15:13 Wangtn 阅读(39) 评论(0) 推荐(0) 编辑
摘要:#include <iostream> #include <fstream> #include <stdio.h> #include <string.h> #include <dirent.h> #include <sys/stat.h> #include <sys/types.h> #includ 阅读全文
posted @ 2022-08-23 14:56 Wangtn 阅读(188) 评论(0) 推荐(0) 编辑
摘要:讲算法原理的有很多,直接贴代码 dijkstra算法是直接对邻接矩阵进行操作求出最短路径的,我项目中的图结构需要转化成邻接矩阵,所以会有下面代码 图结构是一个map,first表示节点的index,second是一个结构体,包含节点的详细信息 map<int, node> routeTable; s 阅读全文
posted @ 2022-06-06 09:58 Wangtn 阅读(52) 评论(0) 推荐(0) 编辑
摘要:目录结构 编译脚本build.sh if [ -d "./proto_code" ];then rm -rf ./proto_code fi mkdir ./proto_code protoc -I ./ --grpc_out=./proto_code --plugin=protoc-gen-grp 阅读全文
posted @ 2022-03-23 17:20 Wangtn 阅读(2900) 评论(0) 推荐(0) 编辑
摘要:自己动手写一个grpc c++的demo,自己写protobuf文件,编译文件和源码 实现一个最简单的grpc功能,客户端向服务端发送一个消息,服务端接收到消息后把结果返回给客户端 demo的文件结构 首先定义proto文件 官方教程:https://developers.google.com/pr 阅读全文
posted @ 2022-03-22 18:05 Wangtn 阅读(5118) 评论(2) 推荐(0) 编辑
摘要:线程库<pthread.h> 获取程序的进程号 getpid() cout<<"main function pid:"<<getpid()<<endl; 获取线程函数的线程号 #include <sys/types.h> cout<<"route thread function pid:"<<get 阅读全文
posted @ 2022-03-10 09:41 Wangtn 阅读(272) 评论(0) 推荐(0) 编辑
摘要:#include <stdio.h> #include <unistd.h> #include <string.h> #include <dirent.h> #include <stdlib.h> #include <limits.h> int main(void) { DIR *dir; stru 阅读全文
posted @ 2022-03-09 15:43 Wangtn 阅读(517) 评论(0) 推荐(0) 编辑
摘要:将线程绑定到cpu指定核心可以避免线程函数在多个核心上执行,从而减少线程间通信的开销,也方便查看负载,便于比较不同线程之间负载是否均衡。 cpu的声明(变量类型)cpu_set_t 绑定进程主要是通过三个函数,这三个函数都是在线程函数里面调用的 CPU_ZERO(&cpu_size_t) cpu初始 阅读全文
posted @ 2022-02-21 16:47 Wangtn 阅读(8311) 评论(0) 推荐(0) 编辑
摘要:void func(void) {} struct Foo{ void operator()(void) {} }; struct Bar{ using fr_t=void (*)(void); static void func(void){} operator fr_t(void) { retur 阅读全文
posted @ 2022-02-10 14:10 Wangtn 阅读(59) 评论(0) 推荐(0) 编辑
摘要:默认参数是静态绑定的,而虚函数是动态绑定的。 默认参数的使用要看指针或者引用本身的类型,而不是对象的类型 #include <iostream> using namespace std; class Base { public: virtual void fun ( int x = 10 ) { c 阅读全文
posted @ 2022-01-26 13:34 Wangtn 阅读(355) 评论(0) 推荐(0) 编辑
摘要:int n=1,m=2; const int* a=&n; cout<<*a<<endl; a=&m; cout<<*a<<endl; int* const b=&n; cout<<*b<<endl; *b=10; cout<<*b<<endl; const int* a中,const修饰变量,表示 阅读全文
posted @ 2022-01-25 10:37 Wangtn 阅读(266) 评论(0) 推荐(0) 编辑
摘要:目录结构 Add.cc是加法,Mul.cc是乘法,main.cc通过条件宏进行调用,在CMakeLists.txt中通过option进行控制 代码 cal.h #ifndef _CAL_H #define _CAL_H #include <iostream> using namespace std; 阅读全文
posted @ 2021-07-26 15:30 Wangtn 阅读(1816) 评论(0) 推荐(0) 编辑
摘要:c程序执行过程就是不同函数互相调用的过程,但是函数调用是有时间和空间开销的,函数调用需要执行入栈出栈操作。如果函数很复杂,执行时间长,那么入栈出栈的操作相比之下可以忽略,但如果函数较简单,那么相比之下入栈出栈的开销就不能忽略了。因此c++提供了一种代码替换的方法,就是内联函数inline。在编译时用 阅读全文
posted @ 2021-06-04 16:52 Wangtn 阅读(421) 评论(0) 推荐(0) 编辑
摘要:size_t fread ( void *ptr, size_t size, size_t count, FILE *fp ); size_t fwrite ( void * ptr, size_t size, size_t count, FILE *fp );用到的函数原型 #include <s 阅读全文
posted @ 2021-06-02 18:10 Wangtn 阅读(131) 评论(0) 推荐(0) 编辑
摘要:#include <stdio.h> int main(int argc,char* argv[]) { FILE* fp; char ch; //printf("number of argc is %d\n",argc); if((fp=fopen(argv[1],"r"))==NULL) { p 阅读全文
posted @ 2021-06-01 16:42 Wangtn 阅读(97) 评论(0) 推荐(0) 编辑
摘要:typedef的作用就是起别名 可以用来给数组,指针,结构体定义别名 1.typedef为数组类型定义别名 #include <stdio.h> typedef char str1[20]; int main(void) { str1 arr; printf("%d",int(sizeof(str1 阅读全文
posted @ 2021-05-31 14:25 Wangtn 阅读(389) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示