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 ...
随机推荐
- 【分块】【线段树】bzoj3212 Pku3468 A Simple Problem with Integers
线段树入门题…… 因为poj原来的代码莫名RE,所以丧病地写了区间修改的分块…… 其实就是块上打标记,没有上传下传之类. #include<cstdio> #include<cmat ...
- python3 开发面试题(去重保持原来的顺序)6.2
""" l1 = [11, 2, 3, 22, 2, 4, 11, 3] 去重并保持原来的顺序 """ #方式一 for 循环方法 l1 = ...
- 小白的Python之路 day4 不同目录间进行模块调用(绝对路径和相对路径)
一.常用模块调用函数功能解释 1.__file__ 功能:返回自身文件的相对路径 你从pycharm的执行结果可以看出,在pycharm执行atm.py文件时,是从绝对路径下去执行的,而你从cmd下去 ...
- Spring中@Value用法收集
一.配置方式 @Value需要参数,这里参数可以是两种形式: @Value("#{configProperties['t1.msgname']}") 或者 @Value(" ...
- [干货]2017已来,最全面试总结——这些Android面试题你一定需要
地址.http://blog.csdn.net/xhmj12/article/details/54730883 相关阅读: 吊炸天!74款APP完整源码! [干货精品,值得收藏]超全的一线互联 ...
- BigDecimal类整除报错的解决方案
例如: BigDecimal num1 = new BigDecimal("10"); BigDecimal num2 = new BigDecimal("3" ...
- scrapy安装使用教程
1. 安装Python,我用的是Python2.7.11,你喜欢用什么版本,你开心就好,只是后面的软件有些可能需要配套. 2. 安装pip,下载pip-8.1.2.tar.gz (md5, pgp). ...
- ubuntu16.04_install_saltstack_更新版本
一.ubuntu16.04版本,安装saltstack 参考官方文档: https://repo.saltstack.com/#ubuntu 二.安装方法 Installs the latest re ...
- Android使用tcpdump抓包
AllJoyn中有个问题:Server切换到Client后,重新加入其他Server时join session会失败,原因是timeout(join session是异步的,在指定时间内没有收到回应) ...
- 阿里云部署Java web项目初体验
林炳文Evankaka原创作品. 转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要讲了怎样在阿里云上安装JDK.Tomcat以及其配置过程. 最后以一个实例来演 ...