【#文档大全网# 导语】以下是®文档大全网的小编为您整理的《编程常用代码》,欢迎阅读!
编程常用代码
编程常用代码计时:#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>(t2-t1); double time0=static_cast(getTickCount());//do somethingtime0=((double)getTickCount()-time0)/getTickFrequency(); clock_t start = clock();//do somethingclock_t finish = clock();double totaltime = (double)(finish - start) / 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类型数据。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);使用智能指针记得初始化:#include pcl::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 something#endif //注释代码块#if 0//do something#endif //函数入口进行参数检查assert(条件/表达式); 遵循doxygen注释规范 小数后面加f2.1(double,8字节)2.1f(flaot,4字节) 浮点定点化(float*2
)>>n for循环并行处理:包含OpenMP的头文件:#include在for循环前面加上一行:#pragma omp parallel for————————————————
本文来源:https://www.wddqxz.cn/cad876f054270722192e453610661ed9ac515595.html