C/C++
文章平均质量分 89
趴在树上写代码的猫
这个作者很懒,什么都没留下…
展开
-
boost::function boost::bind 简单示例
#include // std::cout#include // boost::function#include // boost::bind// 类AAclass CFAA{public: // 测试虚函数的替代 void methodAA() { std::cout << "CFAA methodA() " << std::原创 2013-06-20 14:52:42 · 726 阅读 · 0 评论 -
C++11随机数
#pragma once#include // C++11随机数class random_help{private: std::default_random_engine _engine; std::mt19937_64 _mt19937_gen; std::random_device _rd;private: // noncopyable random_help(ran原创 2016-01-06 15:57:45 · 420 阅读 · 0 评论 -
静态库单例问题
问题:静态库中实现的单例,一个动态库link了该静态库,另一个执行程序同时link了前面的静态库和动态库,会出现什么问题测试环境:window// lib .h#pragma onceclass libx_a{private: int _a;public: int get_a() { return _a; } void set_a(int a) { _a =原创 2016-01-06 16:19:22 · 1160 阅读 · 0 评论 -
腾讯tlog黑盒实现(伪代码)
客户端接口部分,这个编译成库文件// tlog_interface.hnamespace tlog{ class tlog_category_interface; class tlog_ctx_interface;};typedef tlog::tlog_ctx_interface* LPTLOGCTX;typedef tlog::tlog_category_interface*原创 2016-05-19 17:11:40 · 1482 阅读 · 0 评论 -
简易版的随机地图方法,适合少数场景
简易版的随机地图方法,适合少数场景int array_1[][4] = { {1, 1, 1, 1}, {3, 3, 3, 3}, {5, 5, 5, 7},};int array_2[][5] = { {2, 2, 2, 2, 2}, {4, 4, 4, 4, 4}, {6, 6, 6, 6, 6}, {8, 8, 8, 8, 8},}原创 2016-06-17 16:12:33 · 1521 阅读 · 0 评论 -
网状高效排序双向链表
这是一个表头用网状的排序双向链表结构托管类,原本开发的原因是游戏服务器根据玩家战斗力排序的容器更新 & 查找效率不理想,希望有个新的方法替代,因为战斗力变化频率比较高,多个用户又可能是相同的战斗力,所以做了这个类似跳跃表的结构对数据分段enum EN_GRIDDING_SORT{ en_grd_sort_up, en_grd_sort_down,};templatestruct原创 2016-11-02 17:29:13 · 466 阅读 · 0 评论 -
系统内存占用
inline long long sys_get_memory_kb(){#ifdef _MSC_VER PROCESS_MEMORY_COUNTERS MEM; GetProcessMemoryInfo(GetCurrentProcess(), &MEM, sizeof(MEM)); return MEM.WorkingSetSize / 1024;#else std::ifst原创 2016-12-26 11:51:08 · 269 阅读 · 0 评论 -
split
templatevector split(const string& str, const string& delim){ vector ret; size_t off = 0; string::size_type index = str.find_first_of(delim, off); while (index != string::npos) { std::istring原创 2017-01-06 15:15:48 · 295 阅读 · 0 评论 -
时间轮定时器
基础数据结构:event_type _type; // 类型uint64_t _index; // guid(时间戳)uint32_t _repeat; // 重复次数timer_callback _caller; // 回调时间轮结构定义:union timer_clock{ time64_t原创 2018-01-28 15:21:52 · 551 阅读 · 0 评论 -
C++11 std::async
C++11 的std::thread相关解决了不同平台线程调用方式不一致的问题,配合std::function & std::bind lambda ... 定义自己的线程池很方便。新标准还有一个字面意义更直白的异步函数 std::async#include #include #include void test_1(){ auto future = std::a原创 2018-02-06 11:02:59 · 260 阅读 · 0 评论 -
积累-destroy
#pragma once#include <vector>#include <list>#include <set>#include <map>#include <unordered_set>#include <unordered_map>#define SAFE_DELETE(point) do {dele...原创 2018-02-18 16:13:59 · 126 阅读 · 0 评论 -
2017项目 review-message
游戏相关记录:(主要是一些项目经验的总结,记录一下方便查阅)google-protobuf:扩展 & 复合 & 引用,模块化业务逻辑,规范定义,减少相似度高的重复定义sql table maker:key (string) pk -- 自定义名称 eg:create_table_characterversion (string) pk -- 一般是时间戳就好 eg:2018010...原创 2018-02-18 16:57:23 · 321 阅读 · 0 评论 -
安全的文件操作
_extraHandle = fopen(_writeExtra.c_str(), "rb+"); if (!_extraHandle) { _extraHandle = fopen(_writeExtra.c_str(), "wb"); if (_extraHa...原创 2018-06-06 18:03:37 · 209 阅读 · 0 评论 -
敏感词过滤
敏感词过滤&防沉迷,基本所有上线系统都会用的基础功能吧,网上挺多代码的,也很多种方式,看的有点绕写了个自己感觉比较干净清楚的,结构也比较简单,记录一下insert 和 censor(审查、检查)都是递归,效率还好,win release版本5700的样本,length = 1000的censor_str 大概0.2msstruct mgc_node { std::unordere...原创 2018-06-25 14:09:50 · 358 阅读 · 0 评论 -
circular 基于std::array覆盖式
template<class _Ty, size_t _Size>class circular{public: class iterator_circual { public: private: size_t _pos; const circular<_Ty, _Size>* _buf; publ...原创 2018-06-20 17:39:58 · 285 阅读 · 0 评论 -
spdlog
https://github.com/gabime/spdlog/wiki/1.-QuickStart一个不错的log库,C++11标准,跨平台,线程安全别造轮子了转载 2018-07-10 10:39:27 · 1811 阅读 · 0 评论 -
浅谈 C++ 中的 new/delete 和 new[]/delete[]
转载自:https://blog.csdn.net/hazir/article/details/21413833在 C++ 中,你也许经常使用 new 和 delete 来动态申请和释放内存,但你可曾想过以下问题呢?new 和 delete 是函数吗?new [] 和 ...转载 2018-07-23 14:18:22 · 150 阅读 · 0 评论 -
环缓冲托管实现
class x_buffer{private: #pragma pack(1) typedef struct { volatile size_t _rd; volatile size_t _wd; volatile size_t _size; volatile bool _empty; char _data[1]; }x_buffer_data, *x_buffe原创 2015-12-21 16:23:50 · 461 阅读 · 0 评论 -
vim 另存搜索项
:redir > xxx.txt:g/aaa/p:redir ENDredir 指定缓冲区结束写入的文件g/aaa/p搜索并显示包含aaa的行(放入缓冲区)redir END 写入指定文件linux 缓冲区cat > test.txt aaaaaaabbbbbbbccccccover ---- 结束原创 2013-08-31 20:14:12 · 853 阅读 · 0 评论 -
mysql insert into on duplicate key update timestamp类型 (近期遇到问题)
insert into test (updateTs) value (from_unixtime(%d)) update updateTs = values(updateTs)后面Update中的updateTs = 不能加from_unixtimeprintf 等 %d %ll#includeint main() { unsigned long long b原创 2013-07-17 19:54:54 · 1161 阅读 · 0 评论 -
std::map key=std::pair
#include #include using namespace std;typedef pair KEY;KEY getKey(int a, int b){ return make_pair(a, b);}int main(){ cout << "Hello world!" << endl; map mapTest; mapTest[原创 2013-07-31 18:15:01 · 1004 阅读 · 0 评论 -
模拟经典的爬楼梯问题
模拟经典的爬楼梯问题经典描述:一次最多爬2层,当有N层楼梯时候有多少种情况程序:(低效的递归,double容易溢出,别算太大的数)const double step1 = 1;const double step2 = 2;double GetStep(unsigned int num){ if (num == 1) return step1;原创 2013-08-21 16:08:38 · 1214 阅读 · 0 评论 -
Function & #define Function
我们经常定义#define的宏,比如#define MIN(x, y) (((x) 用来返回较小的一个值,很方便。但是如果x、y有一个是函数返回,那么这个函数会被访问两次#define MIN(x, y) (((x) < (y)) ? (x) : (y))int& getint(){ cout Call GetInt FunCall GetInt Fun10"原创 2013-09-12 12:45:35 · 1370 阅读 · 0 评论 -
内存的问题
最近遇到的内存问题比较严重,测试下来win32的程序在32机上最多可以分配出2G的内存,就会bad_alloc掉了。下面是整理的一些查找内存使用的代码。#include struct aaa{ int xxx; float yyy; char zzz;};#define CLIENTBLOCK new(__FILE__, __LINE__)#define NEW CL原创 2013-09-24 13:30:03 · 434 阅读 · 0 评论 -
计算当前时间到星期N之间的时间差
#include #include #include #include using namespace boost::posix_time;using namespace boost::gregorian;const std::string gStartDateTime = "2013-11-25 00:00:00"; // 周日 24点const unsigned int原创 2013-11-28 12:18:11 · 653 阅读 · 0 评论 -
boost 序列化
#include #include #include #include #include #include #include class CTest{ public: bool m_b; char m_c; int m_i; flo原创 2013-12-06 11:49:03 · 752 阅读 · 0 评论 -
正则表达式集
验证数字的正则表达式集 验证数字:^[0-9]*$验证n位的数字:^\d{n}$验证至少n位数字:^\d{n,}$验证m-n位的数字:^\d{m,n}$验证零和非零开头的数字:^(0|[1-9][0-9]*)$验证有两位小数的正实数:^[0-9]+(.[0-9]{2})?$验证有1-3位小数的正实数:^[0-9]+(.[0-9]{1,3})?$验证非零的正整数:^\+?[1-9][转载 2013-12-30 21:31:02 · 386 阅读 · 0 评论 -
适合配置的验证方案
//////////////////////////////////////////////////////////////////////////////// 配置格式:I32| -> int32 类型,值在1、2、3中枚举// I64|[1,100] -> int64 类型,值在[1,100]范围内(全闭区间)// F32|(1.0,11.9) -原创 2013-12-31 17:30:04 · 498 阅读 · 0 评论 -
mac 安装 muduo
最近在看陈硕先生的muduo开源库,很实用的库,书里也有很多可学习的知识。推荐一下。陈硕的博客【http://blog.csdn.net/solstice】muduo的安装依赖cmake,作者的主系统环境是linux,但是也提供了几个patch【http://en.wikipedia.org/wiki/Patch_(Unix)】的文件包含macos。在没有patch diff的情况下mac编译原创 2014-02-14 14:58:18 · 3894 阅读 · 2 评论 -
C++11 lambda表达式
http://www.congci.com/item/cpp-right-value-lambda C++ 11中引入的一个非常重要的概念就是右值引用。理解右值引用是学习“移动语义”(move semantics)的基础。而要理解右值引用,就必须先区分左值与右值。 对左值和右值的一个最常见的误解是:等号左边的就是左值,等号右边的就是右值。左值和右值都是针对表达式而言的,转载 2014-03-22 16:36:19 · 542 阅读 · 0 评论 -
key->value Table方式取值的一种方法 (使用lambda表达式)
// name2id.h#ifndef _NAME2ID_H_#define _NAME2ID_H_struct name2id{const char* name;int id;};int GetIDByName(const char* name);#endif // _NAME2ID_H_// name2id.cpp原创 2014-03-25 16:58:59 · 849 阅读 · 0 评论 -
ENSURE方式的log
//---------------------------------------------//// ENSURE 方式的log//---------------------------------------------//class CEnsureLog{private: mutable std::string m_osLog;public原创 2014-04-06 17:17:55 · 990 阅读 · 0 评论 -
union 应用
struct stVec{union{struct {unsigned int x, y, z;};struct {unsigned int xyz[3];};};};struct stSequence{union {struct {unsigned char x, y, z, w;};struct原创 2014-04-10 10:21:35 · 405 阅读 · 0 评论 -
std::map特性的小技巧
typedef std::pair > KEY;std::map mapTest;void FuncTest(){ auto FnInsert = [&](unsigned int key01, int key02, int value)->void { KEY key = std::make_pair(key01, key02); mapTest[key].insert(va原创 2014-08-23 16:41:44 · 1826 阅读 · 0 评论 -
环缓冲模拟实现
class CLoopBuffer{ private: char* m_pBegin; char* m_pEnd; char* m_pNextRead; char* m_pNextWrite; unsigned int m_nSize; public: CLoopBuffer() : m_pBegin(NULL) , m_pEnd(NULL) , m原创 2015-03-14 17:00:05 · 373 阅读 · 0 评论 -
Mark GetProcAddress Using In Win & Unix
typedef std::string (*foo_t)(const std::string);foo_t foo = NULL;...# ifdef _WIN32 HMODULE hDLL = ::LoadLibrary(szMyLib); if (!hDll) { /*error*/ } foo = (foo_t)::GetProcAddress(hDLL, "foo")转载 2015-05-13 15:29:13 · 330 阅读 · 0 评论 -
近值排序
近值排序近值排序report from :https://codereview.stackexchange.com/questions/48470/std-lib-like-c-function-to-find-nearest-elements-in-a-container近值排序report from :https://codereview.stackexchange.com/questio...转载 2018-11-29 09:34:11 · 148 阅读 · 0 评论