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 ...
随机推荐
- 【最小路径覆盖】【二分图】【最大流】【Dinic】bzoj2150 部落战争
裸的最小路径覆盖. 把每个点拆点,变成二分图. 对于可以连边的点对(i,j):i->j'(1); 对于任意一点i,若i点为'.':S->i(1),i'->T(1); 答案为所有'.' ...
- DataSnap Session expired处理。
测试环境:RAD 10.2.3 建立DataSet Server服务端连接oracle数据库. 1.客户端用FDConnection连接服务端,协议为TCP/IP时,当服务端重启,不用再重启客户端. ...
- Android中选项卡功能的实现
Android中选项卡功能的实现 Android中使用TabHost和TabWidget来实现选项卡功能.TabHost必须是布局的根节点,它包含两个子节点: TabWidget,显示选项卡: Fra ...
- Go beego框架使用笔记(一)
Beego介绍 beego我认为是go初学者比较容易上手的一门MVC Web框架.简单易懂,最重要的一点就是提供了中文文档,这对于我这种英语能力比较差的人来说就是福音. beego的官网上是这么介绍b ...
- 重新认识JavaScript里的创建对象
一.序 面向对象有一个标志,那就是它们都有类的概念,而通过类可以创建任意多个具有相同属性和方法的对象.ECMA-262把对象定义为“无序属性的集合,其属性可以包含基本值.对象或者函数”. 使用Obje ...
- iOS 中json解析数据出现中文乱码的问题
一般服务器的编码格式都是UTF8,这样通过json解析下来的的数据,一般中文是不会出现乱码,但是如果服务器的编码格式不是UTF8,通过json解析的数据中的中文容易出现luan乱码,怎么解决这个问题呢 ...
- PostgreSQL on Linux 最佳部署手册
安装常用包 # yum -y install coreutils glib2 lrzsz mpstat dstat sysstat e4fsprogs xfsprogs ntp readline-de ...
- Linux查找并删除重复文件的命令行fdupes工具,dupeGuru图形工具
查了几十个网页,找到这个接近满意的解决方案http://unix.stackexchange.com/questions/146197/fdupes-delete-files-aft... 不过正则里 ...
- VSM(Virtual Storage Manager For Ceph)安装教程
转载注明出处,陈小跑 http://www.cnblogs.com/chenxianpao/p/5770271.html 一.安装环境 OS:CentOS7.2 VSM:v2.1 released 二 ...
- npm依赖管理:冗余,依赖树
npm的依赖树查询:原理都是查询文件夹node_modules的结构.比如mac的node_modules位置在/usr/local/lib下.具体项目的node_modules位置位于项目根目录下. ...