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)

 1 #include <fstream>
 2 #include <cstdlib>
 3 #include <iostream>
 4 #include <boost/regex.hpp>
 5 
 6 using namespace std;
 7 
 8 int main()
 9 {
10     ifstream ifs("d:/in.txt");
11     ofstream ofs("d:/out.txt");
12     if (ifs && ofs)
13     {
14         int val = 0, tmp = 0;
15         string s, data;
16         getline(ifs, s);
17         getline(ifs, data);
18         // convert to integer
19         val = atoi(s.c_str());
20 
21         boost::regex expression("[0-9]+");
22         boost::smatch what;
23 
24         for ( boost::sregex_iterator iter(data.begin(), data.end(), expression), end; iter != end; ++iter )
25         {
26             /*
27                 http://www.boost.org/doc/libs/1_57_0/libs/regex/doc/html/boost_regex/ref/match_results.html#boost_regex.match_results.subscript
28 
29                 I quote:
30                     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.
31             */
32             if ((*iter)[0].matched)
33             {
34                 tmp = atoi((*iter)[0].str().c_str());
35                 ofs << (double)tmp / (double)val << " ";
36             }
37         }
38         ifs.close();
39         ofs.close();
40     }
41     else
42     {
43         if (!ifs)
44             cout << "cannot open in.txt" << endl;
45         else
46             cout << "cannot create out.txt" << endl;
47     }
48 
49     return 0;
50 }

in.txt

1 36
2 12,35,64

 

out.txt

1 0.333333 0.972222 1.77778 

 

posted @ 2014-11-16 20:42  rldts  阅读(3502)  评论(0编辑  收藏  举报