C++算法系列
I am 006!
code in China make for World
展开
-
欺骗的艺术--骗过编译器,C++类型转换器
欺骗的艺术--骗过编译器,C++类型转换器原创 2023-04-02 09:09:27 · 79 阅读 · 0 评论 -
V4L2取流小结
基于linux V4L2获取摄像头数据原创 2023-01-30 09:34:15 · 599 阅读 · 0 评论 -
C++实现身份证号码过滤与排序
用C++对一组身份证号码进行过滤与排序原创 2022-12-08 09:36:59 · 954 阅读 · 0 评论 -
YUV图像格式转换方法实践
YUV图像格式转换原创 2022-11-23 16:08:04 · 1478 阅读 · 0 评论 -
qnx ipc实践
qnx ipc简介原创 2022-10-24 09:31:25 · 623 阅读 · 0 评论 -
一种pipeline设计模式
一种pipeline设计模式原创 2022-08-10 15:51:59 · 193 阅读 · 0 评论 -
带有彩色log打印
五彩log打印原创 2022-08-08 14:18:36 · 426 阅读 · 0 评论 -
boost::lexical_cast c++ c整数转字符串耗时对比
boost::lexical_cast c++ c整数转字符串耗时对比原创 2022-07-28 10:33:59 · 263 阅读 · 0 评论 -
将NV21图像某一区域变黑色
将图像变黑原创 2022-07-25 16:27:27 · 374 阅读 · 0 评论 -
boost bind嵌套使用
#include <boost/bind/bind.hpp>#include <boost/array.hpp>#include <iostream>#include <string>#include <algorithm>#include <functional>#include <cassert>void sample1() { const boost::array<int, 12> .原创 2022-03-03 13:45:59 · 156 阅读 · 0 评论 -
函数对象好例子
#include <iostream>#include <string>#include <vector>#include <cassert>#include <typeinfo>#include <algorithm>#include <boost/any.hpp>typedef boost::any cell_t;typedef std::vector<cell_t> db_row_t;..原创 2022-02-07 16:50:42 · 342 阅读 · 0 评论 -
boost::thread_group简单使用
类似线程池,demo如下:#include <boost/thread.hpp>#include <boost/bind.hpp>#include <iostream>void fun(int i) { std::cout << "fun:" << i << std::endl;}int main() { boost::thread_group threads; for (auto i = 0;i原创 2021-11-05 16:55:45 · 422 阅读 · 0 评论 -
snort的mSplit函数
一 函数接口以功能// 将一个字符串按照分割字符串进行分割处理后返回字符串指针数组char **mSplit(const char *str, // 原始字符串 如"XXX\t\n122\n222,YY\\UUTY\t111\n\n\\1" const char *sep_chars, // 分割字符串 如" \t,\n" 即按照Tab或逗号或换行进行分割 const int max_toks, // 最多获原创 2021-11-03 14:13:11 · 536 阅读 · 0 评论 -
计算1/(1+1^2+1^4) + 2/(1+2^2+2^4) + ... + 100/(1+100^2+100^4)
一 暴力求解double sum = 0;for (int i = 1;i <= 100;i++) { sum += i / (1.0 + i * i + i * i * i * i);}// sum = 0.4995二 利用数学公式1.找到通项2.转换为平方和、平方差公式3.利用拆项方法,将通项转为两项之和4.合并化简三 测试上述两种方法性能循环1千万次,测试两种方法方法1:耗时0.589s方法1:耗时0.004s可见算法的选择决定了程原创 2021-10-28 14:44:39 · 364 阅读 · 0 评论 -
Z字形变换
一 问题描述将一个给定字符串 s 根据给定的行数 numRows ,以从上往下、从左到右进行Z 字形排列。比如输入字符串为 "PAYPALISHIRING"行数为 3 时,排列如下:P A H NA P L S I I GY I R二 算法简析1.变换规则是Z字形变换,是由下至上,然后右下至上,一下一上为一个周期,循环迭代,如下所示:2.设计二维表std::vector<std::vector<char> >z_array存储变换后的字符串,z_...原创 2021-10-28 10:17:57 · 660 阅读 · 0 评论 -
给定一组数:2/10~9/10,共8个连续数,按照+或-进行组合形成累加和为1的可能性是多少种
一 算法简述设置X为累加和表达式的正数和,设置Y为累加和表达式的负数和,则满足X - Y = 10X + Y = 44(2 + 3 + ... + 9)推导可得X = 27,即只需从这8个连续数找到累加和为27的这些数,比如X可以是2 + 3 + 5 + 8 + 9或2 + 3 + 4 + 5 + 6 + 7等等难点是找到所有解。算法采用随机迭代法,即迭代若干次,随机从8个数选择数进行累加和,检测是否满足累加和为27。二 代码// 2/10,3/10,4/10,5/10,6原创 2021-10-25 19:06:50 · 104 阅读 · 0 评论 -
C++11模板参数类型推导
#include <typeinfo>#include <iostream>template <typename T>void f_ref(T &arg) { std::cout << typeid(arg).name() << std::endl;}template <typename T>void f_pointer(T *arg) { std::cout << typeid(arg.原创 2021-10-14 15:43:07 · 342 阅读 · 0 评论 -
leetcode 有效的数独
#include <string.h>#include <iostream>#include <vector>using namespace std;bool array[10] = { 0 };class Solution {public: bool isValidSudoku(vector<vector<char>>& board) { return judge_rows(board) &&.原创 2021-10-08 19:50:19 · 68 阅读 · 0 评论 -
常见openssl加解密命令
openssl enc -aes-128-cbc -in test.cpp -out test11.cpp -iv f123 -K 1223 #aes-128-cbc为填充模式 -iv指定盐 -K指定秘钥openssl aes-128-cbc -d -in test11.cpp -out test22.cpp -iv f123 -K 1223 #解密sha256sum test.cpp#控制台输出a4a47eb4777020e741c5b3cd6e29fd2325ebd8c13233095f5.原创 2021-09-22 10:34:53 · 2590 阅读 · 0 评论 -
端口简易存储
一 简述 在应用程序中,有时需要存储主机端口并根据相应条件判断某些端口是否在存在。比如snort插件代码中,会根据输入包的端口与配置文件snort.conf里读取的的端口进行匹配操作。如果存储所有的主机端口将浪费一定存储空间,因此,可以设计一种简易的端口存储方式,即可节省存储空间,又能快速识别端口。二 思路分析1.设计一个一维数组ports,其长度是MAX_PORTS(65536)/8;2.某一个端口port在ports的索引是port/8,比如端口8,9,10...15都被存储到por..原创 2021-08-12 17:15:03 · 230 阅读 · 0 评论 -
c++17区域锁std::scoped_lock应用实例
一 std::scoped_lock简介template< class... MutexTypes >class scoped_lock;(1)将多个锁(std::mutex等)包装成一种锁类型,用于线程一次性申请多个锁,避免死锁。(2)当程序出现异常,可自动析构,完成锁的是否。二 实例#include <iostream>#include <string>#include <vector>#include <mutex&原创 2021-08-07 11:09:45 · 4134 阅读 · 0 评论 -
leetcode-罗马数字转整数
typedef struct special_roman { const char *roman; int value;} special_roman;const int SPECIAL_ROMAN_LEN = 6;special_roman romans[SPECIAL_ROMAN_LEN] = { {"IV", 4}, {"IX", 9}, {"XL", 40}, {"XC", 90}, {"CD", 400}, {"CM", 900} };std::map<char, .原创 2021-08-05 09:07:19 · 71 阅读 · 0 评论 -
异步调用std::async实现并行计算
一 std::asynctemplate< class Function, class... Args>std::future<std::result_of_t<std::decay_t<Function>(std::decay_t<Args>...)>> async( Function&& f, Args&&... args );std::async返回一个future对象,可通过g...原创 2021-07-28 14:47:59 · 389 阅读 · 0 评论 -
利用std::is_invocable在编译期间判断函数与传递参数是否匹配
一 可构造性判断std::is_invocabletemplate <class T, class... Args> struct is_constructible;T是待检查的类型,Args为可变参数例如:struct A { A (int,int) { };}; std::cout << "A(int): " << std::is_constructible<A ,int>::value <<原创 2021-07-13 09:02:39 · 2016 阅读 · 0 评论 -
leetcode 旋转图像
https://leetcode-cn.com/problems/rotate-image/#include <iostream>#include <vector>using namespace std;class Solution {public: void rotate(vector<vector<int>>& matrix) { int i = 0, j = 0; int n = matrix原创 2021-07-07 17:09:46 · 51 阅读 · 0 评论 -
安全拷贝函数safe_memcpy实现分析
一 memcpy缺陷分析1.memcpy原型:#include <string.h>void *memcpy(void *dest, const void *src, size_t n); #函数返回dest指针2.缺陷分析:(1)memcpy容易发生缓冲区溢出,如果dest长度小于待拷贝的字节数n,那么程序将发生缓冲区溢出;(2)如果dest是高地址,src是低地址,拷贝可能发生错误,即源地址和目的地址重合部分在拷贝时被修改。...原创 2021-05-28 16:18:56 · 2207 阅读 · 2 评论 -
std::result_of编译时获取函数返回类型(C++11)
一 定义:1.头文件:<type_traits>2.函数定义:原创 2021-05-28 08:34:28 · 3114 阅读 · 1 评论 -
boost创建scoped_thread
一 概述scoped_thread创建的线程在作用域范围外自动join,不需要手动写join函数,如下代码展示了使用scoped_thread创建线程,并使用bind关联线程函数和相关参数,向文件写入一些字符。二 代码#include <fstream>#include <iterator>#include <algorithm>#include <boost/thread.hpp>#include <boost/thread/sc原创 2021-05-08 16:27:13 · 234 阅读 · 0 评论 -
tcmalloc性能测试
一 tcmalloc简介1.tcmalloc(thread-caching malloc),即线程缓存malloc,为每个线程分配本地的cache;2.线程所需的小对象(几十个字节,128KB以下)都从本地cache取;3.同时tcmalloc维护进程级别的cache,大对象从这里取,但会加自旋锁(spinlock),这时会耗费性能;4.如果应用程序需要频繁分配与释放小对象,则可以考虑使用tcmalloc。二 tcmalloc使用应用程序无需修改new或者malloc,只需要在安装t原创 2021-04-17 17:20:25 · 616 阅读 · 0 评论 -
字母异位词分组
class Solution {public: vector<vector<string>> groupAnagrams(vector<string>& strs) { std::vector<std::vector<std::string>>groups; if (strs.empty()) { return groups; } group.原创 2021-04-13 14:57:45 · 55 阅读 · 0 评论 -
使用字节拷贝进行转换--bit_cast
一 概述bit_cast是C++20支持的按字节进行转换的方法,如:auto a = std::bit_cast<std::array<char, sizeof(p)>>(p);二 代码分析1.bit_cast.hpp:#pragma once#include <cstring>#include <type_traits>template <class To, class From>typename std::ena原创 2021-04-12 17:00:07 · 1176 阅读 · 0 评论 -
适合分数的计数排序可以秒数快速排序
#include <iostream>#include <algorithm>#include "count_sort.hpp"int main() { static const int LEN = 10; int loop = 10000000; for (int i = 0;i < loop;i++) { int array[LEN] = { 1, 0, 1, 2, 4, 10, 11, 13, 2, 5 }; .原创 2021-04-01 17:01:39 · 163 阅读 · 0 评论 -
利用lambda调用tuple所有成员
#include <iostream>#include <tuple>#include <algorithm> template <typename Tuple, typename Func, size_t ...N>void apply_func_to_tuple(const Tuple &t, Func &&func, std::index_sequence<N...>) { static_cast&.原创 2021-06-21 09:18:05 · 331 阅读 · 0 评论 -
leetcode删除有序数组重复元素
#include <iostream>#include <vector>class Solution {public: int removeDuplicates(std::vector<int>& nums) { int size = nums.size(); int i = 0; int j = 0; int start = 0; int same_cnt = 0;.原创 2021-03-29 14:46:04 · 47 阅读 · 0 评论 -
leetcode x的平方根
#include <cmath>#include <iostream>class Solution {public: int mySqrt(int x) { if (x <= 0) { return 0; } return int(sqrt(x)); }};int main() { Solution ss; std::cout << ss.mySqrt.原创 2021-03-08 14:11:58 · 40 阅读 · 0 评论 -
syslog异步发送客户端
https://github.com/wangzhicheng2013/syslog_client原创 2021-02-28 19:53:10 · 191 阅读 · 0 评论 -
leetcode电话号码的字母组合
#include <iostream>#include <string>#include <vector>class Solution {public: std::vector<std::string> letterCombinations(std::string digits) { int index = 0; int size = digits.size(); std::string str.原创 2021-01-28 10:35:22 · 106 阅读 · 1 评论 -
简易版日志
https://github.com/wangzhicheng2013/simple_log原创 2021-01-05 10:55:04 · 71 阅读 · 0 评论 -
leetcode两两交换链表节点
#include <iostream>struct ListNode { int val; ListNode *next; ListNode() : val(0), next(nullptr) {} ListNode(int x) : val(x), next(nullptr) {} ListNode(int x, ListNode *next) : val(x), next(next) {}};class Solution {public: .原创 2021-01-03 18:55:44 · 68 阅读 · 0 评论 -
p0f客户端
https://github.com/wangzhicheng2013/p0f_client原创 2020-12-31 15:42:35 · 162 阅读 · 0 评论