编程常用代码 编程常用代码 计时: #includechrono::steady_clock::time_point t1=chrono::steady_clock::now();//do somethingchrono::steady_clock::time_point t2=chrono::steady_clock::now();chrono::duration time_used=chrono::duration_cast(t2t1); double time0=static_cast(getTickCount());//do somethingtime0=((double)getTickCount()time0)/getTickFrequency(); clock_t start = clock();//do somethingclock_t finish = clock();double totaltime = (double)(finishstart) / CLOCKS_PER_SEC; 睡眠: ros::Duration(0.5).sleep(); // sleep for half a second #include sleep(n)//n秒 随机数生成: DUtils::Random::RandomInt(0,n);//随机生成0~n间的整数 std::random_device rd;int x = rd() % 100; int x = std::rand() % 100;float b = 255 * float(rand()) / RAND_MAX; RNG rng;//产生64位整数int N1 = rng;//产生[0,1)范围内均匀分布的double类型数据。 1 3 double N1d = rng.uniform(0.,1.); //产生符合均值为1,标准差为0.5的高斯分布的随机数double N1g = 1 + rng.gaussian(0.5); 高斯噪声生成: cv::RNG rng;double w_sigma=1.0;rng.gaussian(w_sigma) 常量 M_PI (math.h)CV_PI (opencv)DBL_MAX (float.h)(内置宏定义,double最大值) 进度条 printf("\r bar: %m.nf %% \r", value * 100);fflush(stdout); 类的智能指针 #include // 定义class Image {public:using Ptr = std::shared_ptr; // 使用别名}// 使用Image::Ptr image_pointer = Image::Ptr(new Image());Image image;Image::Ptr image_pointer = std::make_shared(image); 使用智能指针记得初始化: #includepcl::PointCloud::Ptr point_cloud_xyzrgb_ = boost::make_shared(); 注:typedef boost::shared_ptr > Ptr; 不然会报错:Assertion `px != 0 failed 编程习惯: //用宏定义(不要写死)#define IMG_WIDTH 640#define IMG_HIGHT 480 //宏定义换行用\ //调试宏#define DEBUG#ifdef DEBUG//do 2 3 something#endif //注释代码块#if 0//do something#endif //函数入口进行参数检查assert(条件/表达式); 遵循doxygen注释规范 小数后面加f2.1(double,8字节)2.1f(flaot,4字节) 浮点定点化(float*2^n)>>n for循环并行处理:包含OpenMP的头文件:#include在for循环前面加上一行:#pragma omp parallel for———————————————— 3 3 本文来源:https://www.wddqw.com/doc/a84e08815afafab069dc5022aaea998fcd22406e.html