摘要:来看这个代码: int fun(int& a) { int b = a; int c = a; return a+b+c; } int main() { int a=1; //.........做一些和a无关的事 return fun(a); } 这个代码是很好优化的,因为编译器知道a的值是1,参考上下文,编译器又能知道b和c的值也是1,而且根本没...
阅读全文
随笔分类 - C/C++基础
C/C++基础特性以及概念
摘要:来看这个代码: int fun(int& a) { int b = a; int c = a; return a+b+c; } int main() { int a=1; //.........做一些和a无关的事 return fun(a); } 这个代码是很好优化的,因为编译器知道a的值是1,参考上下文,编译器又能知道b和c的值也是1,而且根本没...
阅读全文
摘要:#include typedef struct _Node{ int value; _Node *next;}Node; void AddNodeTail(Node *&head, int value){ Node *newNode = new Node; newNode->value = value; newNode->next = NULL; if(head...
阅读全文
摘要:#include using namespace std; class Callee { public: void PrintInfo(int i) { cout (arg); callee->PrintInfo(i); } }; typedef void(*CallbackFunctor)(void*, int); class Caller {...
阅读全文
摘要:gcc提供了大量的警告选项,对代码中可能存在的问题提出警告,通常可以使用-Wall来开启以下警告: -Waddress -Warray-bounds (only with -O2) -Wc++0x-compat -Wchar-subscripts -Wimplicit-int -Wimplicit-function-declaration ...
阅读全文
摘要:#include struct Hello { int helloworld() { return 0; } }; struct Generic {}; // SFINAE test template class has_helloworld { typedef char yes[1]; typedef yes no[2]; template...
阅读全文
摘要:#include // std::copy #include // std::size_t #include class dumb_array { public: // (default) constructor dumb_array(std::size_t size = 0) : mSize(size), mArray(mSize ? ...
阅读全文
摘要:连续内存序列容器(vector, string, deque) 对于连续内存序列STL容器,例如vector,string,deque,删除当前iterator会使得后面所有的iterator都失效,因为它们使用了连续分配的内存,删除一个元素导致后面所有的元素会向前移动一个位置,保证元素的连续性。当上述容器的erase方法可以返回下一个有效的iterator,即erase方法的返回的iterat...
阅读全文
摘要:具体方法 #define offsetoff(TYPE,MEMBER) ( (size_t)( &( ( (TYPE*)0 )->MEMBER ) ) ) 实现解析 (TYPE*)0是一个空指针,如果使用空指针访问成员肯定造成段错误,但是前面的”&”这个符号,表示我们仅仅取MEMBER字段的地址,而不是引用该字段内容,因此不会造成段错误。 另外,结构体中的字段在内存中分配的地址是连续的...
阅读全文
|