introduc.htm 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Language" content="en-us">
  5. <meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
  6. <meta name="GENERATOR" content="Microsoft FrontPage 6.0">
  7. <meta name="ProgId" content="FrontPage.Editor.Document">
  8. <title>Introduction</title>
  9. </head>
  10. <body bgcolor="#FFFFFF">
  11. <p><img src="../../../boost.png" alt="C++ Boost" width="277" height=
  12. "86"/><br></p>
  13. <h1 align="center">Introduction</h1>
  14. <p align="left">The Boost Tokenizer package provides a flexible and
  15. easy-to-use way to break a string or other character sequence into a series
  16. of tokens. Below is a simple example that will break up a phrase into
  17. words.</p>
  18. <div align="left">
  19. <pre>
  20. // simple_example_1.cpp
  21. #include&lt;iostream&gt;
  22. #include&lt;boost/tokenizer.hpp&gt;
  23. #include&lt;string&gt;
  24. int main(){
  25. using namespace std;
  26. using namespace boost;
  27. string s = "This is, a test";
  28. tokenizer&lt;&gt; tok(s);
  29. for(tokenizer&lt;&gt;::iterator beg=tok.begin(); beg!=tok.end();++beg){
  30. cout &lt;&lt; *beg &lt;&lt; "\n";
  31. }
  32. }
  33. </pre>
  34. </div>
  35. <p align="left">You can choose how the string gets parsed by using the
  36. TokenizerFunction. If you do not specify anything, the default
  37. TokenizerFunction is <em>char_delimiters_separator&lt;char&gt;</em> which
  38. defaults to breaking up a string based on space and punctuation. Here is an
  39. example using another TokenizerFunction called
  40. <em>escaped_list_separator</em>. This TokenizerFunction parses a superset
  41. of comma-separated value (CSV) lines. The format looks like this:</p>
  42. <p align="left">Field 1,"putting quotes around fields, allows commas",Field
  43. 3</p>
  44. <p align="left">Below is an example that will break the previous line into
  45. its three fields.</p>
  46. <div align="left">
  47. <pre>
  48. // simple_example_2.cpp
  49. #include&lt;iostream&gt;
  50. #include&lt;boost/tokenizer.hpp&gt;
  51. #include&lt;string&gt;
  52. int main(){
  53. using namespace std;
  54. using namespace boost;
  55. string s = "Field 1,\"putting quotes around fields, allows commas\",Field 3";
  56. tokenizer&lt;escaped_list_separator&lt;char&gt; &gt; tok(s);
  57. for(tokenizer&lt;escaped_list_separator&lt;char&gt; &gt;::iterator beg=tok.begin(); beg!=tok.end();++beg){
  58. cout &lt;&lt; *beg &lt;&lt; "\n";
  59. }
  60. }
  61. </pre>
  62. </div>
  63. <p align="left">Finally, for some TokenizerFunctions you have to pass
  64. something into the constructor in order to do anything interesting. An
  65. example is the offset_separator. This class breaks a string into tokens based
  66. on offsets. For example, when <em>12252001</em> is parsed using offsets of
  67. 2,2,4 it becomes <em>12 25 2001</em>. Below is the code used.</p>
  68. <div align="left">
  69. <pre>
  70. // simple_example_3.cpp
  71. #include&lt;iostream&gt;
  72. #include&lt;boost/tokenizer.hpp&gt;
  73. #include&lt;string&gt;
  74. int main(){
  75. using namespace std;
  76. using namespace boost;
  77. string s = "12252001";
  78. int offsets[] = {2,2,4};
  79. offset_separator f(offsets, offsets+3);
  80. tokenizer&lt;offset_separator&gt; tok(s,f);
  81. for(tokenizer&lt;offset_separator&gt;::iterator beg=tok.begin(); beg!=tok.end();++beg){
  82. cout &lt;&lt; *beg &lt;&lt; "\n";
  83. }
  84. }
  85. </pre>
  86. </div>
  87. <p align="left">&nbsp;</p>
  88. <hr>
  89. <p><a href="http://validator.w3.org/check?uri=referer"><img border="0" src=
  90. "../../doc/images/valid-html401.png" alt="Valid HTML 4.01 Transitional"
  91. height="31" width="88"></a></p>
  92. <p>Revised
  93. <!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B %Y" startspan -->9 June 2010<!--webbot bot="Timestamp" endspan i-checksum="38518" --></p>
  94. <p><i>Copyright &copy; 2001 John R. Bandela</i></p>
  95. <p><i>Distributed under the Boost Software License, Version 1.0. (See
  96. accompanying file <a href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or
  97. copy at <a href=
  98. "http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>)</i></p>
  99. </body>
  100. </html>