随笔分类 -  C++

摘要:在weibo上看到Laruence大神修复了一个使用snprintf的bug (http://t.cn/Rm6AuFh) 引起了TK教主的关注。TK教主着重提到了在windows下snprintf与_snprintf的行为有差别。 想想自己之前也在windows下写过代码,因具体的使用场景没有触发这 阅读全文
posted @ 2018-04-11 15:15 lanyuliuyun 阅读(450) 评论(0) 推荐(0)
摘要:STL内部实现的rbtree,实现 lower_bound/upper_bound 过程,是从 begin() 开始向 end() 进行遍历,将元素的 key 与目标 key 进行比较,直至找到的第一个符合要求的 iterator 为止!具体看代码,如下 位于bits/stl_tree.h 阅读全文
posted @ 2016-06-26 11:08 lanyuliuyun
摘要:shared_ptr::operator->返回的是T*类型指针,非const T*指针。因此通过const shared_ptr&类型的ptr可以直接调用T各个原始的方法,不用担心const与非const问题。具体shared_ptr::operator->实现如下,摘自boost1.52.0版本... 阅读全文
posted @ 2014-07-07 14:39 lanyuliuyun 阅读(1121) 评论(0) 推荐(0)
摘要:首先这是从酷壳网看到陈皓和云风两位大牛们讨论的一个问题,这里记录一下自己的理解。问题如下,先看一段代码 1 #include <iostream> 2 using namespace std; 3 4 class Base 5 { 6 public : 7 Base(){} 8 virtual ~Base(){ 9 cout<<"Base::~Base()"<<endl;10 }11 };12 13 class Derived : public Base14 {15 public:16 Derived(){}17 virtual ~De. 阅读全文
posted @ 2013-05-01 12:52 lanyuliuyun 阅读(1555) 评论(5) 推荐(1)
摘要:C++中除了沿用C的alloc系列函数之外,还可以用new/new []来分配内存(这句是废话),我们在使用new这个operator的时候多是直接使用,而没有额外引用头文件。写C语言代码写习惯的人都会在alloc函数之后,对指针做NULL判断,以检查内存分配是否成功,那么在C++中是否也需要以同样的方式来检查内存分配是否成功呢?根据实际code验证结果,答案是new成功返回即表示内存分配成功,不需检查指针是否为NULL,但是仍然会出现内存分配失败的情况,此时需要捕捉bad_alloc这种exceptin来知晓内存分配失败,bad_alloc是在new这个C++头文件(#include < 阅读全文
posted @ 2013-04-25 00:17 lanyuliuyun 阅读(1211) 评论(0) 推荐(0)
摘要:1 #include 2 #include 3 4 using namespace std; 5 6 int main(int argc, char *argv[]) 7 { 8 binder1st > big = bind1st(greater(), 3); 9 int value = 4;10 11 if (big(value))12 {13 cout > big = bind1st(greater(), 3);这一句,其中拆开来,是由以下几个部分组成greater()是将greater::operator()传给bind1st()... 阅读全文
posted @ 2013-04-10 23:10 lanyuliuyun 阅读(605) 评论(0) 推荐(0)