windows中的visual studio固然强大,但是对于平时测试用的小程序来说,建立一个工程毕竟是麻烦的。相对来说用g++在命令行下进行编译就方便多了(当然也可以用cl.exe),找到了这么一个工具,cygwin-b20,比较小巧实用。从这里下载。
安装及使用方法:
直接使用可执行文件full.exe进行安装,安装目录建议选择默认路径。安装完成后,将路径C:\cygnus\cygwin-b20\H-i586-cygwin32\bin加入的系统环境变量PATH中,这样,你可以在任何目录下执行bin/的命令,其中包含了169个较为常用的linux命令
Archive for June, 2009
June 28, 2009
1 2 3 4 5 6 7 8 9 | class quick_sort { public: typedef int T; explicit quick_sort(T* src, int beg, int end); private: void sub_sort(int beg, int end); T* m_src; }; |
June 27, 2009
boost目前的最新版本是1.39,下载地址:
http://sourceforge.net/project/showfiles.php?group_id=7586&package_id=8041
下载后,解压boost_1_39_0.tar.gz
1 | $ tar -zxvf boost_1_39_0.tar.gz |
然后进入解压缩后的文件夹编译boost的编译器jam
1 2 | $ cd boost_1_39_0\tools\jam $ ./build_dist.sh |
June 26, 2009
1 2 3 4 5 6 7 8 9 10 | #include <iostream> #include <iomanip> //~ #include <sqlite.h> //~ #include <mysql/mysql.h> using namespace std; template <unsigned int x, unsigned int y, bool is_ordered = (x >= y)> //~ 一般情况下x>=y struct static_gcd { static int const value = static_gcd<y, x % y>::value; }; |

这里面有三张函数表,分别对应d的三个基类。事实上,d的虚函数d()的地址保存在了基类B1对应的虚函数表内。从多重继承的内存布局,我们可以看到子类新加入的虚函数被加到了第一个基类的虚函数表,所以当dynamic_cast的时候,子类和第一个基类的地址相同,不需要移动指针,但是当dynamic_cast到其他的父类的时候,需要做相应的指针的移动。
June 24, 2009
在C++程序中,有下面五”类”内存:
- 在栈上创建。在执行函数时,函数内局部变量的存储单元都在栈上创建,函数执行结束时这些存储单元自动被释放。栈内存分配运算内置于处理器的指令集中,一般使用寄存器来存取,效率很高,但是分配的内存容量有限。
- 从堆上分配,亦称动态内存分配。程序在运行的时候用malloc或new申请任意多少的内存,程序员自己负责在何时用free或delete来释放内存。动态内存的生存期由程序员自己决定,使用非常灵活。
- 从静态存储区域分配。内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在。例如全局变量,static变量。
- 文字常量分配在文字常量区,程序结束后由系统释放。
- 程序代码区。
使用优化选项编译:
1 | $ g++ test.cpp -o test -O2 |
此时的运行时间:
real 0m0.008s user 0m0.000s sys 0m0.004s |
so amazing!
g++有四个级别的优化选项,分别对应于 -O1, -O2, -O3, -O4.
比较下面两段代码:
1 2 3 4 | for(int i = 0; i < n; i++) { //~ do something; } |
1 2 3 4 | for(int i = 0; i < n; ++i) { //~ the same thing; } |
June 23, 2009
1 2 3 4 5 6 7 8 9 10 11 | #include <algorithm> #include <cmath> #include <cstdlib> #include <iostream> #include <limits> #include <string> #include <sstream> #include "matrix.hpp" #include "vector.hpp" |