用java的时候觉得挺折腾,回头来弄c++才知道什么叫折腾。。。汗。。。

首先参考我写的这篇文章:http://www.cnblogs.com/qrlozte/p/4100892.html

我从sourceforge把整个boost的zip下载下来以后,我主要是在编译 boost regex的时候出问题了:boost有很多library,regex只是其中一个,怎么编译regex?

当然,第一步是查看文档,找到boost-path/doc/html/index.html打开,翻了半天倒找regex的说明文档(其实可以直接从boost-path/libs/regex/index.html直接找到)

文档里面说:

Building from Source

The Regex library is "just a bunch of source files": nothing special is required to build them.

You can either build the files under boost-path/libs/regex/src/*.cpp as a library, or add them directly to your project. This is particularly useful if you need to use specific compiler options not supported by the default Boost build.

于是找到这个目录,确实发现了不少的cpp文件,直接打开terminal

g++ -c *.cpp

于是报了一大堆找不到头文件的error,一看,是类似于<boost/regex/xxx.hpp>之类的找不到,回过头去看文档,于是,在“Getting Started on Windows”这个页面找到了说明,发现boost-path/boost中全是头文件,该页面说明如下:

This is a sketch of the resulting directory structure:

boost_1_57_0\ .................The “boost root directory”
index.htm .........A copy of www.boost.org starts here
boost\ .........................All Boost Header files
lib\ .....................precompiled library binaries
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

然后就把boost-path/boost这个文件夹拷贝到MinGW/include目录下,也就是说,得到:MinGW/include/boost,从而成为了编译器默认搜索路径的一部分

再次打开terminal:

g++ -c *.cpp // 编译成.o文件
ar -r boost_regex.a *.o // 打包成.a文件(库文件,类似java的jar包,但是要配合该库的头文件才能使用)

好,参照这篇文章的步骤:http://www.cnblogs.com/qrlozte/p/4100892.html

大功告成

用一个例子结尾:读入一个文件,进行计算(in.txt中第二行的数除以第一行的数,并放入out.txt)

 #include <fstream>
#include <cstdlib>
#include <iostream>
#include <boost/regex.hpp> using namespace std; int main()
{
ifstream ifs("d:/in.txt");
ofstream ofs("d:/out.txt");
if (ifs && ofs)
{
int val = , tmp = ;
string s, data;
getline(ifs, s);
getline(ifs, data);
// convert to integer
val = atoi(s.c_str()); boost::regex expression("[0-9]+");
boost::smatch what; for ( boost::sregex_iterator iter(data.begin(), data.end(), expression), end; iter != end; ++iter )
{
/*
http://www.boost.org/doc/libs/1_57_0/libs/regex/doc/html/boost_regex/ref/match_results.html#boost_regex.match_results.subscript I quote:
Effects: Returns a reference to the sub_match object representing the character sequence that matched marked sub-expression n. If n == 0 then returns a reference to a sub_match object representing the character sequence that matched the whole regular expression.
*/
if ((*iter)[].matched)
{
tmp = atoi((*iter)[].str().c_str());
ofs << (double)tmp / (double)val << " ";
}
}
ifs.close();
ofs.close();
}
else
{
if (!ifs)
cout << "cannot open in.txt" << endl;
else
cout << "cannot create out.txt" << endl;
} return ;
}

in.txt

 36
12,35,64

out.txt

 0.333333 0.972222 1.77778 

c++ 使用boost regex库 总结的更多相关文章

  1. Windows下用Mingw编译Boost.Regex库

    下载Boost库,解压. 定位到regex库文件夹下. GCC所对应的MAKEFILE为gcc.mak 进入命令提示符下,输入make -f gcc.mak 这是如果直接按回车执行的话,会出现错误: ...

  2. vs 2005 使用 boost regex

    第一步: Boost 入门及其VS2005下编译boost库  boost.regex库安装指南  深入浅出之正则表达式(一)  C++中三种正则表达式比较(C regex,C ++regex,boo ...

  3. #include <boost/regex.hpp>

    boost C++的正则表达式库boost.regex可以应用正则表达式于C++.正则表达式大大减轻了搜索特定模式字符串的负担,在很多语言中都是强大的功能. boost.regex库中两个最重要的类是 ...

  4. Boost正则表达式库regex常用search和match示例 - 编程语言 - 开发者第2241727个问答

    Boost正则表达式库regex常用search和match示例 - 编程语言 - 开发者第2241727个问答 Boost正则表达式库regex常用search和match示例 发表回复   Boo ...

  5. Windows下如何使用BOOST C++库 .

    Windows下如何使用BOOST C++库 我采用的是VC8.0和boost_1_35_0.自己重新编译boost当然可以,但是我使用了 http://www.boostpro.com/produc ...

  6. 在C++ Builder6上使用Boost正则表达式库

    本文关键词:正则表达式 c++ python 软件 正则表达式是一种模式匹配形式,它通常用在处理的文本程序中.比如我们经常使用的grep工具,还是perl语言都使用了正则表达式. 正则表达式是一种模式 ...

  7. profile对比std::regex与boost::regex的性能

    c++11标准库的regex比boost库的regex之间的性能差距接近5倍,这是为什么?stackflow上也找到一篇post<c++11 regex slower than python&g ...

  8. C++中三种正则表达式比较(C regex,C ++regex,boost regex)

    工作需要用到C++中的正则表达式,以下三种正则可供参考 1,C regex #include <regex.h> #include <iostream> #include &l ...

  9. 如何在WINDOWS下编译BOOST C++库 .

    如何在WINDOWS下编译BOOST C++库 cheungmine 2008-6-25   写出来,怕自己以后忘记了,也为初学者参考.使用VC8.0和boost1.35.0.   1)下载boost ...

随机推荐

  1. 【bitset】hdu4920 Matrix multiplication

    先把两个矩阵全都mod3. S[i][j][k]表示第i(0/1)个矩阵的行/列的第k位是不是j(1/2). 然后如果某两个矩乘对应位上为1.1,乘出来是1: 1.2:2: 2.1:2: 2.2:1. ...

  2. Problem X: 零起点学算法22——华氏摄氏温度转换

    #include<stdio.h> int main() { float f,c; while(scanf("%f",&f)!=EOF) c=*(f-); pr ...

  3. Java高级架构师(一)第05节:TortoiseGit的本地使用

  4. Modernizr使用指南(转)

    HTML5, CSS3以及相关技术(例如canvas和web sockets)带来了非常有用的特性,可以让我们的web程序提升一个新的level.这些新技术允许我们只用HTML,CSS和JavaScr ...

  5. HashMap源码-Basic hash bin node

    /** * Basic hash bin node, used for most entries. (See below for * TreeNode subclass, and in LinkedH ...

  6. JavaScript数组api简单说明

    1.一个数组加上另一个(一些)数组,不会修改原数组只会返回新数组 arrayObject.concat(arrayX,arrayX,......,arrayX) 2.把数组按照指定字符串分离,不会修改 ...

  7. [Android Memory] Android性能测试小工具Emmagee

    转载:http://blog.csdn.net/anlegor/article/details/22895993 Emmagee是网易杭州QA团队开发的用于测试指定android应用性能的小工具.该工 ...

  8. csharp 面向对象编程

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Shap ...

  9. unity backbuffer

    拿unity backbuffer的方法 var  backbuffer = new RenderTargetIdentifier(BuiltinRenderTextureType.None); 这个 ...

  10. apache poi合并单元格设置边框

    HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); //创建一个样式 HSSFCellStyle sty ...