C++
爱吃鱼的喵996
这个作者很懒,什么都没留下…
展开
-
C++ STL 堆
#include<iostream>#include<vector>#include<string>#include<algorithm>using namespace std;class Cmp{public: bool operator()(string a, string b){ return a.length()>=b.length(); }};int main(int argc, char const *argv[]原创 2021-08-11 12:08:50 · 725 阅读 · 0 评论 -
C++11 map实现插入后按value自动排序
#include<iostream>#include<map>#include<vector>using namespace std;struct CmpByKeyLength { bool operator()(const pair<int, int>& k1, const pair<int, int>& k2) { return k1.second > k2.second; //降序排序 }};int原创 2021-08-08 12:48:14 · 898 阅读 · 0 评论 -
Linux C++ Redis安装及使用
配置Redis服务器:下载、解压和编译Redis:$ wget https://download.redis.io/releases/redis-6.2.4.tar.gz$ tar xzf redis-6.2.4.tar.gz$ cd redis-6.2.4$ make现在编译的二进制文件在src 目录中可用 。运行Redis:$ src/redis-server您可以使用内置客户端与 Redis 交互:$ src/redis-cliredis> set foo barOK原创 2021-06-26 12:30:05 · 352 阅读 · 0 评论 -
Linux 如何对http服务器进行压力测试
xxxxxxxxxxxx原创 2021-06-12 10:24:55 · 1127 阅读 · 1 评论 -
Linux C++ 基于libevent实操简单的http协议
已解决传输大文件问题新增post数据接收(处理自定义)及返回server.cpp#include <event2/event.h>#include <event2/listener.h>#include <event2/http.h>#include <event2/buffer.h>#include <event2/keyvalq_struct.h>#include <string.h>#include <.原创 2021-06-11 20:28:36 · 665 阅读 · 0 评论 -
Linux C++ 简单的http协议应用
支持读取html, js, css文件支持读取各类型图片等原创 2021-06-10 14:06:30 · 656 阅读 · 0 评论 -
Linux 基于UDP的简单的socket通信
server.cpp#include <stdio.h>#include <string.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>int main(int argc, char * argv[]) { int server_sockfd; int len;转载 2021-06-08 16:02:10 · 355 阅读 · 0 评论 -
Linux 基于TCP的简单的socket通信
server.cpp#include<stdio.h>#include<string.h>#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>#include<arpa/inet.h>int main(int argc, char const *argv[]){ int server_sockfd; //服务端套接字 int client_转载 2021-06-08 15:46:51 · 267 阅读 · 0 评论 -
Qt简单的窗口
mainwindow.h#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>class MainWindow : public QMainWindow{ Q_OBJECTpublic: MainWindow(QWidget *parent = 0); ~MainWindow();};#endif // MAINWINDOW_Hmain.cpp#include "main转载 2021-05-09 16:48:00 · 212 阅读 · 0 评论 -
libevent接口分析
环境配置与初始化event_base_new/** * Create and return a new event_base to use with the rest of Libevent. * * @return a new event_base on success, or NULL on failure. * * @see event_base_free(), event_base_new_with_config() */EVENT2_EXPORT_SYMBOLstruct ev原创 2021-05-04 11:48:50 · 360 阅读 · 0 评论 -
C++ 十进制数转二进制字符串
#include <iostream>#include <bitset>#include <string>using namespace std;int main(){ int n = 5; string str; bitset<7> bits; //7位长度 bits = n; str = bits.to_string(); cout<<str<<endl;原创 2021-03-21 08:32:10 · 2672 阅读 · 0 评论 -
C++ getline读取
#include<bits/stdc++.h>using namespace std;int main(int argc, char const *argv[]){ string str = "1,2,3,null,null,4,5"; stringstream s(str); string str_node; while(getline(s,str_node,',')){//以逗号作为每次读取结束标志 if(str_node=="null"){原创 2021-03-16 14:58:13 · 656 阅读 · 0 评论 -
C++ 判断一个数是否是整数问题
#include<stdio.h>#include<math.h>int judge(float c){ float d = pow(10,-6.0); if((c-(int)c<d) && (c-(int)c>-d))return 1; else return 0;}int main(int argc, char const *argv[]){ //方式一 float x = 1.001; printf("%d\n", (int原创 2021-02-26 13:51:56 · 4543 阅读 · 0 评论 -
关于sizeof计算的问题
sizeof用于计算数据类型大小一般的数据类型随机器不同,字节大小也不同计算int char之类的类型计算int *, char *之类的类型计算指针类型的字节大小,我这里都是4字节(包括结构体类型指针变量)计算结构体变量大小参考:https://www.cnblogs.com/wlzy/p/5897933.html#include<stdio.h>typedef struct{ char a; int b;} Test;typedef struct{ double原创 2021-02-22 17:15:53 · 247 阅读 · 0 评论 -
关于windows窗口编程过程中编译的错误
Compiling...GetSystemMetrics.cpperror C2065: 'SM_MOUSEWHEELPRESENT' : undeclared identifiererror C2065: 'SM_XVIRTUALSCREEN' : undeclared identifiererror C2065: 'SM_YVIRTUALSCREEN' : undeclared identifiererror C2065: 'SM_CXVIRTUALSCREEN' : undeclared i原创 2021-02-19 22:20:45 · 367 阅读 · 0 评论 -
C++ STL queue简单使用
#include<iostream>#include<queue>using namespace std;int main(int argc, char const *argv[]){ queue<int> q; // ueue<int> q{1,2,3};//错误 for(int i=1; i<10; i++)q.push(i); //只能通过push进行追加 //访问队首队尾元素 cout<<q.front()<&原创 2021-02-19 19:02:26 · 252 阅读 · 0 评论 -
C windows.h 创建窗口
#include<windows.h>#pragma comment(lib, "winmm.lib")LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);int WINAPI WinMain(HINSTANCE hInstance, //hInstance是实例句柄,具体是一个数值 HINSTANCE hPrevInstance, //此参数通常总是定义为NULL PSTR szCmdLine, //用来运行程序的命原创 2021-02-18 22:35:27 · 2299 阅读 · 0 评论 -
第一个Windows程序
#include<windows.h>/*WINAPI WinMain与常规的cpp文件一样,其作为入口函数*/ int WINAPI WinMain(HINSTANCE hInstance, //hInstance是实例句柄,具体是一个数值 HINSTANCE hPrevInstance, //此参数通常总是定义为NULL PSTR szCmdLine, //用来运行程序的命令行,有些Windows程序启动时用它来把文件装入内存 int iCmdShow){/原创 2021-02-18 22:33:17 · 223 阅读 · 1 评论 -
关于windows创建窗口的错误
error LNK2001: unresolved external symbol __imp__PlaySoundA@12]文件开头加入这一段代码#pragma comment(lib, “winmm.lib”)error LNK2001: unresolved external symbol _main[Project] --> [Settings] --> 选择"Link"属性页,在Project Options中将/subsystem:console改成/subsystem:w原创 2021-02-18 21:38:42 · 493 阅读 · 0 评论 -
C 函数不定参数使用
#include<stdio.h>#include<stdarg.h>/*函数参数的传递原理函数参数是以数据结构:栈的形式存取,从右至左入栈.eg:先介绍一下可变参数表的调用形式以及原理:首先是参数的内存存放格式:参数存放在内存的堆栈段中,在执行函数的时候,从最后一个开始入栈。因此栈底高地址,栈顶低地址*///实现多个整数值相加void add(int num, ...){//这里设计是,num代表一共有几个变参 va_list args; int total原创 2021-02-18 19:47:59 · 239 阅读 · 0 评论 -
关于printf及其衍生函数使用
#include<stdio.h>#include <stdarg.h>int vspf(char *fmt, ...){ va_list argptr;//需要引入头文件stdarg.h,argptr表示vspf函数后面有多个参数 int cnt; char buffer[80]; va_start(argptr, fmt); cnt = vsprintf(buffer, fmt, argptr); va_end(argptr); printf("%s\n", b原创 2021-02-18 19:17:51 · 397 阅读 · 0 评论 -
关于使用strlen计算宽字节字符串长度的问题
#include<stdio.h>int main(int argc, char const *argv[]){ char *pc = "Hello!"; printf("%d\n", strlen(pc));//输出6 // 字符串前面加上L表示,字符串的每个字符按照两个字节的方式进行存储 wchar_t *pw = L"Hello!"; printf("%d\n", strlen(pw));//输出1 /*因为若以每个字符两个字节方式进行存储,可以发现"Hello!"被处原创 2021-02-18 16:09:38 · 1688 阅读 · 0 评论 -
C++ 初识template
关于C++的template:template并非一次编译便生出适合所有型别的代码,而是针对被使用的(某个或某组)型别进行编译。这导致一个重要的问题:实际处理 template时,面对template function,你必须先提供它的某个实例,然后才能调用。如此方可通过编译。一个简单的例子:#include<iostream>#include<vector>using namespace std;template <typename T>cons原创 2021-02-09 15:48:04 · 365 阅读 · 0 评论 -
cJSON使用文档——超详细
查了一下没有发现关于cJSON的函数api的使用文档,那就自己看源代码整理一份吧,这比单纯分析源代码貌似有用多了整理一份源代码的使用文档,就不得不首先看下各个函数的定义,防止漏掉一些API,同时也对撰写文档的内容有了大概的了解。cJSON_VersionCJSON_PUBLIC(const char*) cJSON_Version(void); 打印当前cJSON的版本使用:printf("Version: %s\n", cJSON_Version());cJSON_InitHooksC原创 2021-02-05 23:53:37 · 5394 阅读 · 1 评论 -
cJSON源码分析(三)
在构建好一个JSON对象之后,如何访问呢?首先试着将json字符串序列化,并全部打印出来看下结构再说:char * string = "{\"name\":\"xxx\", \"name2\":\"xxx2\"}";cJSON * root = cJSON_Parse(string);//json字符串序列化printf("%s\n", cJSON_Print(root));//json格式化输出看源码了解一下cJSON_Print函数大致实现过程吧(cJSON_Parse函数实现的源码可翻阅原创 2021-02-04 09:42:40 · 358 阅读 · 0 评论 -
cJSON源码分析(二)
在构建好一个JSON对象之后,如何访问呢?首先试着将json字符串序列化,并全部打印出来看下结构再说:char * string = "{\"name\":\"xxx\", \"name2\":\"xxx2\"}";cJSON * root = cJSON_Parse(string);//json字符串序列化printf("%s\n", cJSON_Print(root));//json格式化输出看源码了解一下cJSON_Parse函数大致实现过程吧//用来格式化json字符串所需要的缓存空原创 2021-02-03 23:09:01 · 316 阅读 · 0 评论 -
cJSON源码分析(一)
首先看下cJSON数据结构体(摘自源代码,注释的英文使用机器翻译)/* The cJSON structure: */typedef struct cJSON{ /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem 下一步/上一步允许您遍历数组/对象链。或者,使用GetArraySize/GetArrayItem/Ge原创 2021-02-02 23:44:45 · 420 阅读 · 0 评论 -
C++ 关于字符串分割函数split封装
#include<iostream>#include<string>#include<vector>#include<algorithm>using namespace std;vector<string> split(string T, string P){ vector<string> arr; if(T=="")return arr; unsigned point1 = 0, point2 = 0; if(P==原创 2021-02-02 12:32:04 · 293 阅读 · 0 评论 -
linux下编译生成不同文件——gcc为例
在这里插入代码片原创 2021-01-31 14:46:18 · 975 阅读 · 0 评论 -
cJSON的简单使用
代码下载地址: https://github.com/DaveGamble/cJSON#include "cJSON.c"struct Person{ int age; const char *name;};void printJson(cJSON * root)//以递归的方式打印json的最内层键值对{ for(int i=0; i<cJSON_GetArraySize(root); i++) //遍历最外层json键值对,就是遍历当前层次所有的键值对 {原创 2021-01-29 17:43:53 · 301 阅读 · 0 评论 -
__stdcall、__cdcel和__fastcall定义与区别
__stdcall被这个关键字修饰的函数,其参数都是从右向左通过堆栈传递的(__fastcall 的前面部分由ecx,edx传), 函数调用在返回前要由被调用者清理堆栈。这个关键字主要见于Microsoft Visual C、C++。GNU的C、C++是另外一种修饰方式:attribute((stdcall))__cdecl__cdecl 是C Declaration的缩写(declaration,声明),表示C语言默认的函数调用方法:所有参数从右到左依次入栈,这些参数由调用者清除,称为手动清原创 2021-01-29 15:59:09 · 745 阅读 · 0 评论 -
C++ string 找出字符第n次出现的位置
#include<iostream>#include<string>#include<algorithm>using namespace std;/* T:要搜索的串 P:要匹配的串*/int pos(string T, string P, int n){ if(n==0)return -1;//相当于没找 int count = 0; unsigned begined = 0; while((begined=T.find(P,begined))!原创 2021-01-16 16:48:32 · 2882 阅读 · 0 评论 -
C++ string vector 转换
1.string转vector#include<iostream>#include<string>#include<cstring>#include<algorithm>using namespace std;int main(){ string str = "hello world!"; vector<uint8_t> vec; vec.assign(str.begin(), str.end()); for(size_t i=原创 2021-01-16 16:34:16 · 7231 阅读 · 1 评论 -
C++ vector 去重
#include<iostream>#include<vector>#include<algorithm>using namespace std;int main(int argc, char const *argv[]){ vector<int> vec={1,5,2,3,3,4,1,5}; sort(vec.begin(), vec.end()); vec.erase(unique(vec.begin(), vec.end()), vec.原创 2021-01-16 16:33:46 · 486 阅读 · 0 评论 -
基于递归的组合C(n, m)
算法思想:对于一组数1,2,3…n如果要取出m个数作为一组,求每组的组合数。则可以按如下方法:先取第一个数,则有n种取法,用循环实现。比如先取1,然后递归到下一层取第二个数的时候,只有n-1种取法,用循环实现。比如可以取2,然后递归到下一层取第三个数的时候,只有n-2种取法,用循环实现。比如可以取3,然后递归到下一层当取够m个数的时候,就是一种组合,可以输出。/*组合本例是从5个数里面找出n(1<=n<=5)个数组成一组*/#include<iostream>原创 2020-12-14 22:32:01 · 622 阅读 · 0 评论 -
基于递归的全排列
算法思想:对于一组数1,2,3…n;取第一个数1的时候,只有一种排序方法,即1。取第二个数2的时候,有两种排序方法,就是将2插入1的前面或后面,结果是12,21.取第三个数3的时候,有6种排序方法。对于之前已经排好的12,3一共有3个空隙可以插入,结果是312,132,123,对于21也是(321,231,213),所以总共有6种结果。其他的以此类推…/*全排列*/#include<iostream>#include<vector>using na原创 2020-12-14 20:59:30 · 225 阅读 · 0 评论 -
好理解的基于二叉树的0-1背包求解问题
问题描述:给定n(n<=100)种物品和一个背包。物品i的重量是wi,价值为vi,背包的容量为C(C<=1000)。问:应如何选择装入背包中的物品,使得装入背包中物品的总价值最大? 在选择装入背包的物品时,对每种物品i只有两个选择:装入或不装入。不能将物品i装入多次,也不能只装入部分物品i。输入样例1:在这里给出一组输入。例如:5 102 62 36 55 44 6输出样例1:15输入样例2:6 6015 3217 3720 4612 269 2114 30原创 2020-12-05 10:48:41 · 921 阅读 · 0 评论 -
C++ 判断素数的代码
#include<iostream>#include<sstream>#include<cmath>using namespace std;bool isPrime(int k){ int i; int j=(int)sqrt((double)k); for(i=2;i<=j;i++){ if(k%i==0)return false; } return true;}int main(int argc, char co原创 2020-11-26 13:26:37 · 2542 阅读 · 1 评论 -
C++ string和char类型字符串转换代码
#include<iostream>#include<string>#include<cstring>using namespace std;int main(int argc, char const *argv[]){ //char转string //直接赋值 string s; const char *a = "1234"; // char *a = "1234"; char b[] = "56789"; s = a; cout<<原创 2020-11-24 22:52:12 · 384 阅读 · 1 评论 -
C++ 数字与字符串之间的转换代码
#include<iostream>#include<string>#include<sstream>using namespace std;int main(int argc, char const *argv[]){ //字符串转数字 string str1="1234"; int i; stringstream ss1; ss1<<str1; ss1>>i; cout<<i<<endl;原创 2020-11-24 22:15:21 · 351 阅读 · 0 评论