Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/tokenizer/introduc.htm @ 12

Last change on this file since 12 was 12, checked in by landauf, 17 years ago

added boost

File size: 3.4 KB
Line 
1<html>
2
3<head>
4<meta http-equiv="Content-Type"
5content="text/html; charset=iso-8859-1">
6<meta name="GENERATOR" content="Microsoft FrontPage Express 2.0">
7<title>Introduction</title>
8</head>
9
10<body bgcolor="#FFFFFF">
11
12<p><img src="../../boost.png" alt="C++ Boost" width="277"
13height="86"> <br>
14</p>
15
16<h1 align="center">Introduction</h1>
17
18<p align="left">The boost Tokenizer package provides a flexible
19and easy to use way to break of a string or other character
20sequence into a series of tokens. Below is a simple example that
21will break up a phrase into words.</p>
22<div align="left">
23
24<pre>// simple_example_1.cpp
25#include&lt;iostream&gt;
26#include&lt;boost/tokenizer.hpp&gt;
27#include&lt;string&gt;
28
29int main(){
30   using namespace std;
31   using namespace boost;
32   string s = &quot;This is,  a test&quot;;
33   tokenizer&lt;&gt; tok(s);
34   for(tokenizer&lt;&gt;::iterator beg=tok.begin(); beg!=tok.end();++beg){
35       cout &lt;&lt; *beg &lt;&lt; &quot;\n&quot;;
36   }
37}</pre>
38</div>
39
40<p align="left">You can choose how the string gets broken up. You
41do this by specifying the TokenizerFunction. If you do not
42specify anything, the default TokenizerFunction is
43char_delimiters_separator&lt;char&gt; which defaults to breaking
44up a string based on space and punctuation. Here is an example of
45using another TokenizerFunction called escaped_list_separator.
46This TokenizerFunction parses a superset of comma separated value
47(csv) lines. The format looks like this</p>
48
49<p align="left">Field 1,&quot;putting quotes around fields,
50allows commas&quot;,Field 3</p>
51
52<p align="left">Below is an example that will break the previous
53line into its 3 fields</p>
54<div align="left">
55
56<pre>// simple_example_2.cpp
57#include&lt;iostream&gt;
58#include&lt;boost/tokenizer.hpp&gt;
59#include&lt;string&gt;
60
61int main(){
62   using namespace std;
63   using namespace boost;
64   string s = &quot;Field 1,\&quot;putting quotes around fields, allows commas\&quot;,Field 3&quot;;
65   tokenizer&lt;escaped_list_separator&lt;char&gt; &gt; tok(s);
66   for(tokenizer&lt;escaped_list_separator&lt;char&gt; &gt;::iterator beg=tok.begin(); beg!=tok.end();++beg){
67       cout &lt;&lt; *beg &lt;&lt; &quot;\n&quot;;
68   }
69}</pre>
70</div>
71
72<p align="left">Finally, for some TokenizerFunctions you have to
73pass in something into the constructor in order to do anything
74interesting. An example is offset_separator. This class breaks a
75string into tokens based on offsets for example</p>
76
77<p align="left">12252001 when parsed using offsets of 2,2,4
78becomes 12 25 2001. Below is an example to parse this.</p>
79<div align="left">
80
81<pre>// simple_example_3.cpp
82#include&lt;iostream&gt;
83#include&lt;boost/tokenizer.hpp&gt;
84#include&lt;string&gt;
85
86int main(){
87   using namespace std;
88   using namespace boost;
89   string s = &quot;12252001&quot;;
90   int offsets[] = {2,2,4};
91   offset_separator f(offsets, offsets+3);
92   tokenizer&lt;offset_separator&gt; tok(s,f);
93   for(tokenizer&lt;offset_separator&gt;::iterator beg=tok.begin(); beg!=tok.end();++beg){
94       cout &lt;&lt; *beg &lt;&lt; &quot;\n&quot;;
95   }
96}</pre>
97</div>
98
99<p align="left">&nbsp;</p>
100
101<hr>
102
103<p>© Copyright John R. Bandela 2001. Permission to copy, use,
104modify, sell and distribute this document is granted provided
105this copyright notice appears in all copies. This document is
106provided &quot;as is&quot; without express or implied warranty,
107and with no claim as to its suitability for any purpose.</p>
108
109<p align="left">&nbsp;</p>
110</body>
111</html>
Note: See TracBrowser for help on using the repository browser.