代码改变世界

随笔分类 -  Ryan的编程之路

Python中的函数默认参数

2014-01-17 21:23 by Ryan_Liu, 7035 阅读, 收藏,
摘要: 今天想编写个Trie树的小东西,没想到居然遇到python中的一个很知名的坑,看似没毛病的代码,就是得不到正确结果,最后在stackoverflow上才寻得大牛解释。故记录如下。大致是这样一个问题def fun(a=[]): a.append(1) print a#执行3次fun() 预料结果应该是[1] [1] [1] 实际结果如下fun() #[1]fun() #[1,1]fun() #[1,1,1]...这个问题叫做default mutable parameter values,为何会发生这样的结果呢?不急,举个简单的例子def fun1(a): a.append(1) pri... 阅读全文

C++中的#pragma pack效果

2014-01-15 23:25 by Ryan_Liu, 885 阅读, 收藏,
摘要: 先说pragma,参考msdn,A "pragma" instructs the compiler to perform a particular action at compile time. Pragmas vary from compiler to compiler.再来说pragma pack这个制定字节对其的指令,如果制定了字节对齐,那么编译器将按照指定好的指令来生成代码,对性能将会造成一定影响。编译器的默认优化,有点空间换取时间的感觉。。#pragma packinstructs the compiler to pack structure members wi 阅读全文

Linux小工具bc使用

2014-01-08 09:16 by Ryan_Liu, 1097 阅读, 收藏,
摘要: 现在才知道bc竟然是可以编程的,不得不佩服linux各种工具的博大精深(推荐一本书,software toolsman bc,bc可加的参数有bc [ -hlwsqv ],其中我们常用的也就是-l了,它的意思是定义一些常用的数学库,比如log,sin之类的,所以一般打开bc的命令为bc -l常用的数学运算的书写方式s (x) The sine of x, x is in radians.c (x) The cosine of x, x is in radians.a (x) The arctangent of x, arctangent returns radians.l (x) The na 阅读全文