• C++ 常用代码段整理


    1、检查内存泄漏:

    头文件

    // MS Visual C++ memory leak debug tracing
    #if defined(_MSC_VER) && defined(_DEBUG)
    #  define _CRTDBG_MAP_ALLOC
    #  include <crtdbg.h>
    #endif

    source文件

    // MS Visual C++ memory leak debug tracing
    #if defined(_MSC_VER) && defined(_DEBUG)
        _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
    #endif

     

    2、使用c++11 新特性:

    // C++11 override
    #if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ >= 5)) 
      || (defined(__clang__) && (defined (__cplusplus)) && (__cplusplus >= 201103L)) 
      || defined(__CPPCHECK__)
    #  define OVERRIDE override
    #else
    #  define OVERRIDE
    #endif
    
    // C++11 noexcept
    #if (defined(__GNUC__) && (__GNUC__ >= 5)) 
      || (defined(__clang__) && (defined (__cplusplus)) && (__cplusplus >= 201103L)) 
      || defined(__CPPCHECK__)
    #  define NOEXCEPT noexcept
    #else
    #  define NOEXCEPT
    #endif
    
    // C++11 noreturn
    #if (defined(__GNUC__) && (__GNUC__ >= 5)) 
      || (defined(__clang__) && (defined (__cplusplus)) && (__cplusplus >= 201103L)) 
      || defined(__CPPCHECK__)
    #  define NORETURN [[noreturn]]
    #else
    #  define NORETURN
    #endif

    3、获取当前时间的time stamp.  获取的是当前区时下的当前时间。(秒级)

        time_t now = time(0);
        tm ltm;
        localtime_s(&ltm, &now);
        std::string sRet;
        char sRetBuf[80];
        sprintf_s(sRetBuf, sizeof(sRetBuf),
            "%4d/%02d/%02d %02d:%02d:%02d",
            (1900 + ltm.tm_year), (1 + ltm.tm_mon), (ltm.tm_mday),
            ltm.tm_hour, ltm.tm_min, ltm.tm_sec);        

    gmtime_s 和localtime_s 区别:

    (1).gmtime将time_t转换为UTC时间,UTC的全称为Coordinated Universal Time,即世界标准时间。

    (2).localtime将time_t转换为本地时间(local time)。北京时间比UTC时间早8小时。

    Note: 获取毫秒级的当前时间可以用getsystemtime();

            SYSTEMTIME st;
            GetSystemTime(&st);
  • 相关阅读:
    关于iOS开发property with 'retain(or strong)' attribute must be of object type
    机器学习之神经网络
    一些知名的开源社区
    机器学习之正则化
    机器学习之逻辑回归(logistic回归)
    机器学习之正规方程法
    机器学习之线性回归、多项式回归
    机器学习之梯度下降法
    64位windows7下安装python,配置numpy和matplotlib库
    mysql分区查询
  • 原文地址:https://www.cnblogs.com/taofengfeng/p/14343998.html
Copyright © 2020-2023  润新知