zoukankan      html  css  js  c++  java
  • 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);
  • 相关阅读:
    Java-数据结构与算法-选择排序与冒泡排序
    Java-马士兵设计模式学习笔记-迭代器模式-模仿Collectin ArrayList LinckedList
    Java-马士兵设计模式学习笔记-装饰者模式
    1072 Gas Station (30)(30 分)
    1034 Head of a Gang (30)(30 分)
    poj 3723 Conscription
    qduoj 218 签到题
    1045 Favorite Color Stripe (30)(30 分)
    1068 Find More Coins (30)(30 分)
    1057 Stack (30)(30 分)
  • 原文地址:https://www.cnblogs.com/taofengfeng/p/14343998.html
Copyright © 2011-2022 走看看