免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 850 | 回复: 0
打印 上一主题 下一主题

泛型技术和Boost库 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-22 08:51 |只看该作者 |倒序浏览

泛型技术和面向对象技术填补了逻辑上的空缺,从代码来讲,一个是竖向累积,一个是横向扩展。

Boost在STL的基础上发展,成为了一个集泛型技术之大成者的库。

hpp,其实质就是将.cpp的实现代码混入.h头文件当中,定义与实现都包含在同一文件,则该类的调用者只需要include该hpp文件即可,无需再将cpp加入到project中进行编译。
而实现代码将直接编译到调用者的obj文件中,不再生成单独的obj.
采用hpp将大幅度减少调用project中的cpp文件数与编译次数,也不用再发布烦人的lib与dll,因此非常适合用来编写公用的开源库。

使用boost(Linux)

最好的资料是package里的index.htm

1, 下载解压到 ~/bin/boost/boost_1_47_0
    目录如下,

boost_1_47_0/ .................The “boost root directory” index.htm ........A copy of www.boost.org starts here boost/ .....................All Boost Header files libs/ .....Tests, .cpps, docs, etc., by library index.html ........Library documentation starts here algorithm/ any/ array/ …more libraries… status/ .........................Boost-wide test suite tools/ ....Utilities, e.g. Boost.Build, quickbook, bcp more/ ..........................Policy documents, etc. doc/ ...............A subset of all Boost library docs

2, The first thing many people whant to know is, “how do I build Boost?”.
    The good news is that often, there’s nothing to build.
   
The only Boost libraries that must be built separately are:
    Filessystem, GraphParallel, IOStreams, MPI, ProgramOptions, Python, Regex, … Thread…

3, Build a Simple Program using Boost with a header-only library

    To keep things simple, let’s start by using a header-only library.
    /* example.cpp – illustrate the Boost */
    #include <boost/lambda/lambda.hpp>
    #include <iostream>
    #include <iterator>
    #include <algorithm>
    
    int main() {
      using namespace boost::lambda;
      typedef std::istream_iterator<int> in;

      std:for_each(in(std::cin), in(), std::cout << (_1 * 3) << “ ”);
    }

    Now, built it.
    $g++ –I ~/bin/boost/boost_1_47_0 example.cpp –o example

    To test the result type,
    $./example
    $1 2 3
    The result is,
    $3 6 9

4, 全编译,为了使用某些库
    $./bootstrap.sh –help (查看编译选项)
    $./bootstrap.sh --preifx=/home/huang/bin/boost/built (安装路径(必须是绝对路径)/include & /lib, 默认编译器是gcc, user-config.jam)
    $./b2 install (开始编译,并install 库)

    编译成功后,我们就可以使用这个目录来编译应用程序。而不再需要 Boost root directory目录了,
    因为全部的include和lib都已经编译和拷贝到这个目录了。

5, 一个需要库的例子,Regex

    /* 取email的subject, It uses Regex. */
    #include <boost/regex.hpp>
    #include <iostream>
    #include <string>

    int main() {
      std:string line;
      boost::regex pat(“subject: (Re: |Aw: )*(.*)”);
      while (std::cin) {
        std::getline(std::cin, line);
        boost::smatch matchs;
        if (boost::regex_match(line, matchs, pat)) {
          std::cout << matchs[2] << std::endl;
        }
      }
    }

    $g++ –I ~/bin/boost/built/inlcude –L ~/bin/boost/built/lib –lboost-regex-gcc43-mt-d Regex.cpp –o Regex

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP