c++ 使用boost regex库 总结
用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库 总结的更多相关文章
- Windows下用Mingw编译Boost.Regex库
下载Boost库,解压. 定位到regex库文件夹下. GCC所对应的MAKEFILE为gcc.mak 进入命令提示符下,输入make -f gcc.mak 这是如果直接按回车执行的话,会出现错误: ...
- vs 2005 使用 boost regex
第一步: Boost 入门及其VS2005下编译boost库 boost.regex库安装指南 深入浅出之正则表达式(一) C++中三种正则表达式比较(C regex,C ++regex,boo ...
- #include <boost/regex.hpp>
boost C++的正则表达式库boost.regex可以应用正则表达式于C++.正则表达式大大减轻了搜索特定模式字符串的负担,在很多语言中都是强大的功能. boost.regex库中两个最重要的类是 ...
- Boost正则表达式库regex常用search和match示例 - 编程语言 - 开发者第2241727个问答
Boost正则表达式库regex常用search和match示例 - 编程语言 - 开发者第2241727个问答 Boost正则表达式库regex常用search和match示例 发表回复 Boo ...
- Windows下如何使用BOOST C++库 .
Windows下如何使用BOOST C++库 我采用的是VC8.0和boost_1_35_0.自己重新编译boost当然可以,但是我使用了 http://www.boostpro.com/produc ...
- 在C++ Builder6上使用Boost正则表达式库
本文关键词:正则表达式 c++ python 软件 正则表达式是一种模式匹配形式,它通常用在处理的文本程序中.比如我们经常使用的grep工具,还是perl语言都使用了正则表达式. 正则表达式是一种模式 ...
- profile对比std::regex与boost::regex的性能
c++11标准库的regex比boost库的regex之间的性能差距接近5倍,这是为什么?stackflow上也找到一篇post<c++11 regex slower than python&g ...
- C++中三种正则表达式比较(C regex,C ++regex,boost regex)
工作需要用到C++中的正则表达式,以下三种正则可供参考 1,C regex #include <regex.h> #include <iostream> #include &l ...
- 如何在WINDOWS下编译BOOST C++库 .
如何在WINDOWS下编译BOOST C++库 cheungmine 2008-6-25 写出来,怕自己以后忘记了,也为初学者参考.使用VC8.0和boost1.35.0. 1)下载boost ...
随机推荐
- 【二分法】【尺取法】bzoj2348 [Baltic 2011]Plagiarism
一开始以为死于精度……调了半天发现死于long long…… 一.二分法: #include<cstdio> #include<cstring> #include<alg ...
- python操作mysql封装成类
import pymysqlimport loggingimport sys # 加入日志#获取logger实例logger = logging.getLogger("baseSpider& ...
- WebSocket 实战(转)
WebSocket 实战 本文介绍了 HTML5 WebSocket 的由来,运作机制及客户端和服务端的 API 实现,重点介绍服务端(基于 Tomcat7)及客户端(基于浏览器原生 HTML5 AP ...
- 6.2(java学习笔记)字节流
一.FileInputStream 文件输入流从文件中获取输入字节.简单的说就是读取文件. 1.构造方法 FileInputStream(File file)//根据指定文件对象创建一个输入流 2.常 ...
- Asp.Net MVC part6 WebAPI
两种web服务SOAP风格:基于方法,产品是WebServiceREST风格:基于资源,产品是WebAPI可以返回json.xml类型的数据对于数据的增.删.改.查,提供相对的资源操作,按照请求的类型 ...
- python函数中的关键字参数
关键字参数: 就是在形式参数中必须要提供”传递参数名=传递参数值” 位置参数: 仅仅只有参数名 特点:1.位置参数只能出现在关键字参数之前,不管是在行参还是实参中. 2.关键字参数在调用时(实参)中 ...
- dwz中弹出的窗口页面如何获取前页面(点击按钮的页面)的元素???
在页面A.jsp中点击一个按钮,使用$.pdialog.open()方法弹出b.jsp页面(对话框窗口),我要在b.jsp中选中值然后关闭窗口(b.jsp)返回值给A.jsp~ =========== ...
- WCF服务测试工具
官网地址:http://www.wcfstorm.com/wcf/home.aspx WCFStorm Lite 来进行接口查看及调试,如下所示.
- GIS空间参考及坐标转换
空间参考(Spatial Reference)是 GIS 数据的骨骼框架,能够将我们的数据定位到相应的位置,为地图中的每一点提供准确的坐标. 在同一个地图上显示的地图数据的空间参考必须是一致的,如果两 ...
- Docker实践3: Docker常用命令(未完)
查看容器及运行进程 docker ps 查看容器内部信息 docker inspect container_id 进入容器 docker attach container_id 退出容器 docker ...