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) //变量类型