• c++ STL中一些常用函数/模板


    std::floor         //-->向下取整数
    std::ceil          // -->向上取整数:
    std::llround     //最接近的整数
    std::numeric_limits<double>::epsilon() //最小双精度小数
    std::numeric_limits<int>::max()           //最大值
    std::numeric_limits<int>::min()           //最大值
    
    std::ref           //用于std::bind 通过引用传递
    std::cref        //用于std::bind 通过常引用传递
    
    void foo(int& a) {
        a = 10;
    }
    
    void run_func(std::function<void(void)> fun) {
        if (fun) {
            fun();
        }
    }
    int a = 0;
    run_func(std::bind(foo, std::ref(a));
    
    std::vector<int> a = {1,2,3,4,5,6};
    std::sort(a.begin(), a.end(), std::greater<int>());//内置类型的由大到小排序
    std::sort(a.begin(), a.end(), std::less<int>());   //内置类型的由小到大排序
    
    #define BIT_SET(a,b) ((a) |= (1ULL<<(b)))
    #define BIT_CLEAR(a,b) ((a) &= ~(1ULL<<(b)))
    #define BIT_FLIP(a,b) ((a) ^= (1ULL<<(b)))
    #define BIT_CHECK(a,b) (!!((a) & (1ULL<<(b)))) 
    
    
    /* 将vector src的内存空洞清除 */
    void vector_swap() {
        std::vector<int> src = {4,5,6,7,1,2,3};
    
        LOG(INFO) << "v size = "<< src.capacity(); //7
        src.erase(src.begin());
        LOG(INFO) << "v size = "<< src.capacity(); //7
    
        std::vector<int>(src).swap(src);
        LOG(INFO) << "v size = "<< src.capacity(); //6
    }
    
    /* 清空vector的内存*/
    void vector_del() {
        std::vector<int> src = {4,5,6,7,1,2,3};
    
        LOG(INFO) << "v size = "<< src.capacity(); //7
        
        std::vector<int>().swap(src);
        
        LOG(INFO) << "v size = "<< src.capacity(); //0
    }

    std::typeid(double) //变量类型
  • 相关阅读:
    RxJava简要分析
    okHttp3源码简要分析
    Android 内存优化浅析
    用暴走漫画写使用手册
    简单的JavaScript互斥锁
    为jQuery添加Webkit的触摸方法支持
    去年做了什么?OA。
    简单地总结下双十一对“老婆”的评价
    麦当劳送薯条活动坑薯条设想,实践被失败
    用.NET MVC实现长轮询,与jQuery.AJAX即时双向通信
  • 原文地址:https://www.cnblogs.com/rayfloyd/p/14306631.html
Copyright © 2020-2023  润新知