boost是一个开源的c++库,离c++标准只有一步之 遥的库.昨天重装了boost,于是就有了这篇文章.
cd n:/programme
//下载boost
N:/programme>cvs -d:pserver:anonymous@boost.cvs.sourceforge.net:/cvsroot/boost login
N:/programme>cvs -z3 -d:pserver:anonymous@boost.cvs.sourceforge.net:/cvsroot/boost co -P boost
//编译bjam
N:/programme>cd boost/tools/jam
N:/programme/boost/tools/jam>build_dist
...
N:/programme/boost/tools/jam>cd src/bin.ntx86
N:/programme/boost/tools/jam/src/bin.ntx86>dir/b
bjam.exe
N:/programme/boost/tools/jam/src/bin.ntx86>copy b* n:/programme/boost
N:/programme/boost/tools/jam/src/bin.ntx86>cd n:/programme/boost
//按不同编译器修改-sTools=后面的值具体参见文档,--prefix是安装的目标位置,默认是c:/boost或/usr/local
N:/programme/boost>bjam "-sTOOLS=vc-8_0" --prefix=n:/programme/libs/boost
//不晓得编了多少时间,诂计2个多钟头吧
N:/programme/boost>cd ../libs/boost
//编译成功
N:/programme/libs/boost>dir /b
include
lib
N:/programme/libs/boost>
把include,和lib路径加入vs
随便写个测试代码检测一下是否安装成功
#include <boost/bind.hpp>
#include <boost/smart_ptr.hpp>
#include <string>
#include <vector>
#include <iostream>
#include <ctime>
#include <iterator>
#include <algorithm>
using namespace std;
using namespace boost;
template<typename T>
class myvec:public vector<T>
{
public :
myvec(void )
{
cout<<this<<" created."<< endl;
}
~myvec(void )
{
cout<<this<<" destroyed."<< endl;
}
void push_back(const T& str)
{
vector<T> ::push_back(str);
cout<<this<<" push_back "<<str<< endl;
}
void show(void )
{
copy(begin(),end(),ostream_iterator<T>(cout," " ));
cout<< flush;
}
};
typedef myvec<string> vec_string;
bool myless(const string& left,const string& right)
{
return atoi(left.c_str())< atoi(right.c_str());
}
int main(int argc,char** argv)
{
srand((unsigned int )time(NULL));
shared_ptr<vec_string> ptr(new vec_string); //shared_ptr引用计数+1 类似COM的AddRef
for(int i=0;i<10;i++ )
{
ptr->push_back(lexical_cast<string>(rand()%10000)); //兜了个圈子,这里int转成string,一会比较时还要atoi
}
cout<<"sorting"<< endl;
sort(ptr->begin(),ptr->end(),boost::bind(myless,_1,_2));//bind
ptr-> show();
return 0 ;
}//shared_ptr引用计数-1,且为0,vec_string从内存中delete
//输出
003A57B8 created.
003A57B8 push_back 6959
003A57B8 push_back 6874
003A57B8 push_back 2690
003A57B8 push_back 3038
003A57B8 push_back 32
003A57B8 push_back 3205
003A57B8 push_back 471
003A57B8 push_back 9282
003A57B8 push_back 61
003A57B8 push_back 890
sorting
32
61
471
890
2690
3038
3205
6874
6959
9282
003A57B8 destroyed.
测试是成功的.
我的Blog是: http://blog.csdn.net/antter
Email: jmiwork@yahoo.com
QQ: 185500511