C++类的静态函数是属于类型的,而不是某个对象的,因此把它声明成Virtual也没什么实际意义。那么运行下面这段程序,会产生什么样的结果呢?
1 2 3 4 5 6 7 8 | #include <iostream> using namespace std; class Base { public: static void foo() { cout<<"Base"<<endl; } virtual void bar() { foo(); } }; |
C++类的静态函数是属于类型的,而不是某个对象的,因此把它声明成Virtual也没什么实际意义。那么运行下面这段程序,会产生什么样的结果呢?
1 2 3 4 5 6 7 8 | #include <iostream> using namespace std; class Base { public: static void foo() { cout<<"Base"<<endl; } virtual void bar() { foo(); } }; |
想一下,下面的代码片段会输出什么?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <iostream> class Test { public: void print() const { std::cout<<_int<<std::endl; } private: static int _int; }; int Test::_int = 0; int main(int argc, char **argv) { Test TArray[10]; TArray[100].print(); return 0; } |
May you note something, anyhow.
1 2 3 4 5 6 7 8 9 10 11 12 13 | void fun1(char * str) {} void fun2(char ** str) {} //~ int main(int argc, char ** argv) int main(int argc, char * argv[]) { char str1[] = "Hello, Piggy!"; char str2[][4] = "Hello, Piggy!"; fun1(str1); fun2(str2); fun2(&str1); return 0; } |
上面的snippet有错误吗?有几个?你能找出来并说出原因吗?4、5两行有区别吗?想一下,然后看gcc给出的错误信息,
1 2 3 4 5 6 7 | int main() { char * str = new char[32]; str = "Hello, Piggy!"; return 0; } |
这样是会内存泄漏的……而我一直都不知道……不过想来也自然,因为这样是允许的,char *str = “Hello, Piggy!”;,“程序中的字符串被存放在常量存储区”不要把这句话当成耳旁风,谨记。
对最简单的SDBMHash(char*)做了一个简单的测试。随机生成200个长度为31的字符串,利用SDBM得到一个Hash值,然后用直接取余法插入长度为113的HashSet,为简单起见,只是将HashSet对应的位置进行加一。
1 2 3 4 5 6 7 8 9 10 11 | // SDBM Hash Function unsigned int SDBMHash (char *str) { unsigned int hash = 0; while (*str) { hash = (*str++) + (hash << 6) + (hash << 16) - hash; } return (hash & 0x7FFFFFFF); } |
这里只给出一个简单的实现,具体原理参见Google、Baidu的各个角落及各大教材。做两点说明:
仅仅实现了必要的左旋、右旋处理,左平衡、右平衡操作,以及数据的插入操作,
不可重复插入数据。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | typedef int Type; //~ 节点数据类型 typedef struct Node { Type data; int bf; Node *left, *right; Node(){ bf = 0; left = right = 0; } } AVLNode, *AVLTree; //~ 平衡树及其节点 void RRotate(AVLTree & T) //~ 右旋处理,T的左孩子成为新子树根 { AVLNode * tmp = T->left; T->left = tmp->right; tmp->right = T; T = tmp; } |
这个排序算法为什么被命名为Shell排序,我一直幻想着它可能最早被用以Shell脚本吧,Just Now我才知道,发明人叫做Donald Shell,居然有人叫自己壳?
以前从来没有写过这个排序,事实证明,没写过基本等于没听说过、没学过。
21行的小程序花了我将近一个小时来调试,对一个大小为9的逆序用gdb跟踪了程序整个的执行过程,发现两处错误,迭代变量j的类型习惯性地写成了size_t,导致死循环,以后凡是这种i,j,k,一律int。还有就是把最外层的while(step > 0)写成了while(size > 0),纯属自作自受。
看Chris对内联的总结,说到内联函数在编译时会被放入”符号表”,想一探究竟,结果却发现另外一个问题,疑惑不解。
1 2 3 4 5 6 7 | #include <stdio.h> inline int add(int a, int b) { return a + b; } int main() { int n = add(1,2); return 0; } |
就在刚刚过去的2009年11月11日,举国欢庆的光棍节,Google推出了一个全新的系统编程语言Go Program Language. ……Simple……Fast……Safe……Concurrent……Funny……Open Source……似乎很牛B的样子,观望……期待……祝福