//要实现以后的方便移植,并且实现x64和x86的编译不报错,那肯定要先定义一些通用类型。
//需要包含的头文件
#include <iostream> #include <fstream> #include <sstream> #include <string> #include <algorithm> #include <iterator> #include <cctype> #include <vector> #include <iomanip> #include <functional> #include <random> #include <cmath> #include <cassert>
通用类型以及一些有用的宏
typedef char int8;//兼容char, int8_t, signed char typedef uint8_t u_int8; typedef int16_t int16; typedef uint16_t u_int16; typedef int32_t int32; typedef uint32_t u_int32; typedef int64_t int64; typedef uint64_t u_int64; typedef long double ldouble; #define MAKEINT16(x, y) ((u_int16)(((u_int8)(((u_int32)(x)) & 0xff)) | ((u_int16)((u_int8)(((u_int32)(y)) & 0xff))) << 8)) #define MAKEINT32(x,y) ((u_int32)(((u_int16)(((u_int32)(x)) & 0xffff)) | ((u_int32)((u_int16)(((u_int32)(y)) & 0xffff))) << 16)) #define MAKEINT64(x,y) ((u_int64)(((u_int32)(((u_int64)(x)) & 0xffffffff)) | ((u_int64)((u_int32)(((u_int64)(y)) & 0xffffffff))) << 32)) #define LOWINT16(l) ((u_int8)(((u_int16)(l)) & 0xff)) #define HIGHINT16(l) ((u_int8)((((u_int16)(l)) >> 8) & 0xff)) #define LOWINT32(l) ((u_int16)(((u_int32)(l)) & 0xffff)) #define HIGHINT32(l) ((u_int16)((((u_int32)(l)) >> 16) & 0xffff)) #define LOWINT64(l) ((u_int32)(((u_int64)(l)) & 0xffffffff)) #define HIGHINT64(l) ((u_int32)((((u_int64)(l)) >> 32) & 0xffffffff)) #define radian(deg) ((M_PI / 180) * deg) #define degree(rad) ((180 / M_PI) * rad) #ifdef _WIN64 typedef signed __int64 integer; typedef unsigned __int64 u_integer; #define MAKEINT(x, y) MAKEINT64(x,y) #define HIGHINT(l) HIGHINT64(l) #define LOWINT(l) LOWINT64(l) #else typedef signed __int32 integer; typedef unsigned __int32 u_integer; #define MAKEINT(x, y) MAKEINT32(x,y) #define HIGHINT(l) HIGHINT32(l) #define LOWINT(l) LOWINT32(l) #endif inline ldouble max(ldouble a, ldouble b) { return a > b ? a : b; } inline ldouble min(ldouble a, ldouble b){ return a < b ? a : b; }
两个经常用到的随机函数,不用伪随机了,而是用c++新特性里面的真正的随机函数
inline ldouble random_ldouble(ldouble low = 0.0, ldouble high = 1.0) { std::uniform_real_distribution<ldouble> distribution(low, high); std::mt19937 generator; static std::function<ldouble()> fdouble = std::bind(distribution, generator); return fdouble(); } inline integer random_integer(integer low = 0, integer high = std::numeric_limits<integer>::max()) { std::uniform_int_distribution<integer> distribution(low, high); std::mt19937 generator; static std::function<integer()> finteger = std::bind(distribution, generator); return finteger(); }
我把这个文件命名为Types.h,方便以后写通用32位和64位的程序使用。并且我把关键代码全放到了lib里面(新建项目时,选择静态lib就行了),方便其他项目调用。效果如下图