tutorial.html 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <meta name="generator" content="Docutils 0.14: http://docutils.sourceforge.net/" />
  7. <title>Boost Pointer Container Library</title>
  8. <style type="text/css">
  9. /*
  10. :Author: David Goodger (goodger@python.org)
  11. :Id: $Id: html4css1.css 7952 2016-07-26 18:15:59Z milde $
  12. :Copyright: This stylesheet has been placed in the public domain.
  13. Default cascading style sheet for the HTML output of Docutils.
  14. See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to
  15. customize this style sheet.
  16. */
  17. /* used to remove borders from tables and images */
  18. .borderless, table.borderless td, table.borderless th {
  19. border: 0 }
  20. table.borderless td, table.borderless th {
  21. /* Override padding for "table.docutils td" with "! important".
  22. The right padding separates the table cells. */
  23. padding: 0 0.5em 0 0 ! important }
  24. .first {
  25. /* Override more specific margin styles with "! important". */
  26. margin-top: 0 ! important }
  27. .last, .with-subtitle {
  28. margin-bottom: 0 ! important }
  29. .hidden {
  30. display: none }
  31. .subscript {
  32. vertical-align: sub;
  33. font-size: smaller }
  34. .superscript {
  35. vertical-align: super;
  36. font-size: smaller }
  37. a.toc-backref {
  38. text-decoration: none ;
  39. color: black }
  40. blockquote.epigraph {
  41. margin: 2em 5em ; }
  42. dl.docutils dd {
  43. margin-bottom: 0.5em }
  44. object[type="image/svg+xml"], object[type="application/x-shockwave-flash"] {
  45. overflow: hidden;
  46. }
  47. /* Uncomment (and remove this text!) to get bold-faced definition list terms
  48. dl.docutils dt {
  49. font-weight: bold }
  50. */
  51. div.abstract {
  52. margin: 2em 5em }
  53. div.abstract p.topic-title {
  54. font-weight: bold ;
  55. text-align: center }
  56. div.admonition, div.attention, div.caution, div.danger, div.error,
  57. div.hint, div.important, div.note, div.tip, div.warning {
  58. margin: 2em ;
  59. border: medium outset ;
  60. padding: 1em }
  61. div.admonition p.admonition-title, div.hint p.admonition-title,
  62. div.important p.admonition-title, div.note p.admonition-title,
  63. div.tip p.admonition-title {
  64. font-weight: bold ;
  65. font-family: sans-serif }
  66. div.attention p.admonition-title, div.caution p.admonition-title,
  67. div.danger p.admonition-title, div.error p.admonition-title,
  68. div.warning p.admonition-title, .code .error {
  69. color: red ;
  70. font-weight: bold ;
  71. font-family: sans-serif }
  72. /* Uncomment (and remove this text!) to get reduced vertical space in
  73. compound paragraphs.
  74. div.compound .compound-first, div.compound .compound-middle {
  75. margin-bottom: 0.5em }
  76. div.compound .compound-last, div.compound .compound-middle {
  77. margin-top: 0.5em }
  78. */
  79. div.dedication {
  80. margin: 2em 5em ;
  81. text-align: center ;
  82. font-style: italic }
  83. div.dedication p.topic-title {
  84. font-weight: bold ;
  85. font-style: normal }
  86. div.figure {
  87. margin-left: 2em ;
  88. margin-right: 2em }
  89. div.footer, div.header {
  90. clear: both;
  91. font-size: smaller }
  92. div.line-block {
  93. display: block ;
  94. margin-top: 1em ;
  95. margin-bottom: 1em }
  96. div.line-block div.line-block {
  97. margin-top: 0 ;
  98. margin-bottom: 0 ;
  99. margin-left: 1.5em }
  100. div.sidebar {
  101. margin: 0 0 0.5em 1em ;
  102. border: medium outset ;
  103. padding: 1em ;
  104. background-color: #ffffee ;
  105. width: 40% ;
  106. float: right ;
  107. clear: right }
  108. div.sidebar p.rubric {
  109. font-family: sans-serif ;
  110. font-size: medium }
  111. div.system-messages {
  112. margin: 5em }
  113. div.system-messages h1 {
  114. color: red }
  115. div.system-message {
  116. border: medium outset ;
  117. padding: 1em }
  118. div.system-message p.system-message-title {
  119. color: red ;
  120. font-weight: bold }
  121. div.topic {
  122. margin: 2em }
  123. h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,
  124. h4.section-subtitle, h5.section-subtitle, h6.section-subtitle {
  125. margin-top: 0.4em }
  126. h1.title {
  127. text-align: center }
  128. h2.subtitle {
  129. text-align: center }
  130. hr.docutils {
  131. width: 75% }
  132. img.align-left, .figure.align-left, object.align-left, table.align-left {
  133. clear: left ;
  134. float: left ;
  135. margin-right: 1em }
  136. img.align-right, .figure.align-right, object.align-right, table.align-right {
  137. clear: right ;
  138. float: right ;
  139. margin-left: 1em }
  140. img.align-center, .figure.align-center, object.align-center {
  141. display: block;
  142. margin-left: auto;
  143. margin-right: auto;
  144. }
  145. table.align-center {
  146. margin-left: auto;
  147. margin-right: auto;
  148. }
  149. .align-left {
  150. text-align: left }
  151. .align-center {
  152. clear: both ;
  153. text-align: center }
  154. .align-right {
  155. text-align: right }
  156. /* reset inner alignment in figures */
  157. div.align-right {
  158. text-align: inherit }
  159. /* div.align-center * { */
  160. /* text-align: left } */
  161. .align-top {
  162. vertical-align: top }
  163. .align-middle {
  164. vertical-align: middle }
  165. .align-bottom {
  166. vertical-align: bottom }
  167. ol.simple, ul.simple {
  168. margin-bottom: 1em }
  169. ol.arabic {
  170. list-style: decimal }
  171. ol.loweralpha {
  172. list-style: lower-alpha }
  173. ol.upperalpha {
  174. list-style: upper-alpha }
  175. ol.lowerroman {
  176. list-style: lower-roman }
  177. ol.upperroman {
  178. list-style: upper-roman }
  179. p.attribution {
  180. text-align: right ;
  181. margin-left: 50% }
  182. p.caption {
  183. font-style: italic }
  184. p.credits {
  185. font-style: italic ;
  186. font-size: smaller }
  187. p.label {
  188. white-space: nowrap }
  189. p.rubric {
  190. font-weight: bold ;
  191. font-size: larger ;
  192. color: maroon ;
  193. text-align: center }
  194. p.sidebar-title {
  195. font-family: sans-serif ;
  196. font-weight: bold ;
  197. font-size: larger }
  198. p.sidebar-subtitle {
  199. font-family: sans-serif ;
  200. font-weight: bold }
  201. p.topic-title {
  202. font-weight: bold }
  203. pre.address {
  204. margin-bottom: 0 ;
  205. margin-top: 0 ;
  206. font: inherit }
  207. pre.literal-block, pre.doctest-block, pre.math, pre.code {
  208. margin-left: 2em ;
  209. margin-right: 2em }
  210. pre.code .ln { color: grey; } /* line numbers */
  211. pre.code, code { background-color: #eeeeee }
  212. pre.code .comment, code .comment { color: #5C6576 }
  213. pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
  214. pre.code .literal.string, code .literal.string { color: #0C5404 }
  215. pre.code .name.builtin, code .name.builtin { color: #352B84 }
  216. pre.code .deleted, code .deleted { background-color: #DEB0A1}
  217. pre.code .inserted, code .inserted { background-color: #A3D289}
  218. span.classifier {
  219. font-family: sans-serif ;
  220. font-style: oblique }
  221. span.classifier-delimiter {
  222. font-family: sans-serif ;
  223. font-weight: bold }
  224. span.interpreted {
  225. font-family: sans-serif }
  226. span.option {
  227. white-space: nowrap }
  228. span.pre {
  229. white-space: pre }
  230. span.problematic {
  231. color: red }
  232. span.section-subtitle {
  233. /* font-size relative to parent (h1..h6 element) */
  234. font-size: 80% }
  235. table.citation {
  236. border-left: solid 1px gray;
  237. margin-left: 1px }
  238. table.docinfo {
  239. margin: 2em 4em }
  240. table.docutils {
  241. margin-top: 0.5em ;
  242. margin-bottom: 0.5em }
  243. table.footnote {
  244. border-left: solid 1px black;
  245. margin-left: 1px }
  246. table.docutils td, table.docutils th,
  247. table.docinfo td, table.docinfo th {
  248. padding-left: 0.5em ;
  249. padding-right: 0.5em ;
  250. vertical-align: top }
  251. table.docutils th.field-name, table.docinfo th.docinfo-name {
  252. font-weight: bold ;
  253. text-align: left ;
  254. white-space: nowrap ;
  255. padding-left: 0 }
  256. /* "booktabs" style (no vertical lines) */
  257. table.docutils.booktabs {
  258. border: 0px;
  259. border-top: 2px solid;
  260. border-bottom: 2px solid;
  261. border-collapse: collapse;
  262. }
  263. table.docutils.booktabs * {
  264. border: 0px;
  265. }
  266. table.docutils.booktabs th {
  267. border-bottom: thin solid;
  268. text-align: left;
  269. }
  270. h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,
  271. h4 tt.docutils, h5 tt.docutils, h6 tt.docutils {
  272. font-size: 100% }
  273. ul.auto-toc {
  274. list-style-type: none }
  275. </style>
  276. </head>
  277. <body>
  278. <div class="document" id="boost-pointer-container-library">
  279. <h1 class="title"><img alt="Boost" src="boost.png" /> Pointer Container Library</h1>
  280. <h2 class="subtitle" id="tutorial">Tutorial</h2>
  281. <p>The tutorial shows you the most simple usage of the
  282. library. It is assumed that the reader is familiar
  283. with the use of standard containers. Although
  284. the tutorial is devided into sections, it is recommended
  285. that you read it all from top to bottom.</p>
  286. <ul class="simple">
  287. <li><a class="reference internal" href="#basic-usage">Basic usage</a></li>
  288. <li><a class="reference internal" href="#indirected-interface">Indirected interface</a></li>
  289. <li><a class="reference internal" href="#sequence-containers">Sequence containers</a></li>
  290. <li><a class="reference internal" href="#associative-containers">Associative containers</a></li>
  291. <li><a class="reference internal" href="#null-values">Null values</a></li>
  292. <li><a class="reference internal" href="#cloneability">Cloneability</a></li>
  293. <li><a class="reference internal" href="#new-functions">New functions</a></li>
  294. <li><a class="reference internal" href="#compatible-smart-pointer-overloads">Compatible smart pointer overloads</a></li>
  295. <li><a class="reference internal" href="#algorithms">Algorithms</a></li>
  296. </ul>
  297. <div class="section" id="basic-usage">
  298. <h1>Basic usage</h1>
  299. <p>The most important aspect of a pointer container is that it manages
  300. memory for you. This means that you in most cases do not need to worry
  301. about deleting memory.</p>
  302. <p>Let us assume that we have an OO-hierarchy of animals</p>
  303. <pre class="literal-block">
  304. class animal : <a class="reference external" href="http://www.boost.org/libs/utility/utility.htm#Class_noncopyable">boost::noncopyable</a>
  305. {
  306. public:
  307. virtual ~animal() {}
  308. virtual void eat() = 0;
  309. virtual int age() const = 0;
  310. // ...
  311. };
  312. class mammal : public animal
  313. {
  314. // ...
  315. };
  316. class bird : public animal
  317. {
  318. // ...
  319. };
  320. </pre>
  321. <p>Then the managing of the animals is straight-forward. Imagine a
  322. Zoo:</p>
  323. <pre class="literal-block">
  324. class zoo
  325. {
  326. boost::ptr_vector&lt;animal&gt; the_animals;
  327. public:
  328. void add_animal( animal* a )
  329. {
  330. the_animals.push_back( a );
  331. }
  332. };
  333. </pre>
  334. <p>Notice how we just pass the class name to the container; there
  335. is no <tt class="docutils literal">*</tt> to indicate it is a pointer.
  336. With this declaration we can now say:</p>
  337. <pre class="literal-block">
  338. zoo the_zoo;
  339. the_zoo.add_animal( new mammal(&quot;joe&quot;) );
  340. the_zoo.add_animal( new bird(&quot;dodo&quot;) );
  341. </pre>
  342. <p>Thus we heap-allocate all elements of the container
  343. and never rely on copy-semantics.</p>
  344. </div>
  345. <div class="section" id="indirected-interface">
  346. <h1>Indirected interface</h1>
  347. <p>A particular feature of the pointer containers is that
  348. the query interface is indirected. For example,</p>
  349. <pre class="literal-block">
  350. boost::ptr_vector&lt;animal&gt; vec;
  351. vec.push_back( new animal ); // you add it as pointer ...
  352. vec[0].eat(); // but get a reference back
  353. </pre>
  354. <p>This indirection also happens to iterators, so</p>
  355. <pre class="literal-block">
  356. typedef std::vector&lt;animal*&gt; std_vec;
  357. std_vec vec;
  358. ...
  359. std_vec::iterator i = vec.begin();
  360. (*i)-&gt;eat(); // '*' needed
  361. </pre>
  362. <p>now becomes</p>
  363. <pre class="literal-block">
  364. typedef boost::ptr_vector&lt;animal&gt; ptr_vec;
  365. ptr_vec vec;
  366. ptr_vec::iterator i = vec.begin();
  367. i-&gt;eat(); // no indirection needed
  368. </pre>
  369. </div>
  370. <div class="section" id="sequence-containers">
  371. <h1>Sequence containers</h1>
  372. <p>The sequence containers are used when you do not need to
  373. keep an ordering on your elements. You can basically
  374. expect all operations of the normal standard containers
  375. to be available. So, for example, with a <tt class="docutils literal">ptr_deque</tt>
  376. and <tt class="docutils literal">ptr_list</tt> object you can say:</p>
  377. <pre class="literal-block">
  378. boost::ptr_deque&lt;animal&gt; deq;
  379. deq.push_front( new animal );
  380. deq.pop_front();
  381. </pre>
  382. <p>because <tt class="docutils literal"><span class="pre">std::deque</span></tt> and <tt class="docutils literal"><span class="pre">std::list</span></tt> have <tt class="docutils literal">push_front()</tt>
  383. and <tt class="docutils literal">pop_front()</tt> members.</p>
  384. <p>If the standard sequence supports
  385. random access, so does the pointer container; for example:</p>
  386. <pre class="literal-block">
  387. for( boost::ptr_deque&lt;animal&gt;::size_type i = 0u;
  388. i != deq.size(); ++i )
  389. deq[i].eat();
  390. </pre>
  391. <p>The <tt class="docutils literal">ptr_vector</tt> also allows you to specify the size of
  392. the buffer to allocate; for example</p>
  393. <pre class="literal-block">
  394. boost::ptr_vector&lt;animal&gt; animals( 10u );
  395. </pre>
  396. <p>will reserve room for 10 animals.</p>
  397. </div>
  398. <div class="section" id="associative-containers">
  399. <h1>Associative containers</h1>
  400. <p>To keep an ordering on our animals, we could use a <tt class="docutils literal">ptr_set</tt>:</p>
  401. <pre class="literal-block">
  402. boost::ptr_set&lt;animal&gt; set;
  403. set.insert( new monkey(&quot;bobo&quot;) );
  404. set.insert( new whale(&quot;anna&quot;) );
  405. ...
  406. </pre>
  407. <p>This requires that <tt class="docutils literal"><span class="pre">operator&lt;()</span></tt> is defined for animals. One
  408. way to do this could be</p>
  409. <pre class="literal-block">
  410. inline bool operator&lt;( const animal&amp; l, const animal&amp; r )
  411. {
  412. return l.name() &lt; r.name();
  413. }
  414. </pre>
  415. <p>if we wanted to keep the animals sorted by name.</p>
  416. <p>Maybe you want to keep all the animals in zoo ordered wrt.
  417. their name, but it so happens that many animals have the
  418. same name. We can then use a <tt class="docutils literal">ptr_multimap</tt>:</p>
  419. <pre class="literal-block">
  420. typedef boost::ptr_multimap&lt;std::string,animal&gt; zoo_type;
  421. zoo_type zoo;
  422. std::string bobo = &quot;bobo&quot;,
  423. anna = &quot;anna&quot;;
  424. zoo.insert( bobo, new monkey(bobo) );
  425. zoo.insert( bobo, new elephant(bobo) );
  426. zoo.insert( anna, new whale(anna) );
  427. zoo.insert( anna, new emu(anna) );
  428. </pre>
  429. <p>Note that must create the key as an lvalue
  430. (due to exception-safety issues); the following would not
  431. have compiled</p>
  432. <pre class="literal-block">
  433. zoo.insert( &quot;bobo&quot;, // this is bad, but you get compile error
  434. new monkey(&quot;bobo&quot;) );
  435. </pre>
  436. <p>If a multimap is not needed, we can use <tt class="docutils literal"><span class="pre">operator[]()</span></tt>
  437. to avoid the clumsiness:</p>
  438. <pre class="literal-block">
  439. boost::ptr_map&lt;std::string,animal&gt; animals;
  440. animals[&quot;bobo&quot;].set_name(&quot;bobo&quot;);
  441. </pre>
  442. <p>This requires a default constructor for animals and
  443. a function to do the initialization, in this case <tt class="docutils literal">set_name()</tt>.</p>
  444. <p>A better alternative is to use <a class="reference external" href="../../assign/index.html">Boost.Assign</a>
  445. to help you out. In particular, consider</p>
  446. <ul class="simple">
  447. <li><a class="reference external" href="../../assign/doc/index.html#ptr_push_back">ptr_push_back(), ptr_push_front(), ptr_insert() and ptr_map_insert()</a></li>
  448. <li><a class="reference external" href="../../assign/doc/index.html#ptr_list_of">ptr_list_of()</a></li>
  449. </ul>
  450. <p>For example, the above insertion may now be written</p>
  451. <pre class="literal-block">
  452. boost::ptr_multimap&lt;std::string,animal&gt; animals;
  453. using namespace boost::assign;
  454. ptr_map_insert&lt;monkey&gt;( animals )( &quot;bobo&quot;, &quot;bobo&quot; );
  455. ptr_map_insert&lt;elephant&gt;( animals )( &quot;bobo&quot;, &quot;bobo&quot; );
  456. ptr_map_insert&lt;whale&gt;( animals )( &quot;anna&quot;, &quot;anna&quot; );
  457. ptr_map_insert&lt;emu&gt;( animals )( &quot;anna&quot;, &quot;anna&quot; );
  458. </pre>
  459. </div>
  460. <div class="section" id="null-values">
  461. <h1>Null values</h1>
  462. <p>By default, if you try to insert null into a container, an exception
  463. is thrown. If you want to allow nulls, then you must
  464. say so explicitly when declaring the container variable</p>
  465. <pre class="literal-block">
  466. boost::ptr_vector&lt; boost::nullable&lt;animal&gt; &gt; animals_type;
  467. animals_type animals;
  468. ...
  469. animals.insert( animals.end(), new dodo(&quot;fido&quot;) );
  470. animals.insert( animals.begin(), 0 ) // ok
  471. </pre>
  472. <p>Once you have inserted a null into the container, you must
  473. always check if the value is null before accessing the object</p>
  474. <pre class="literal-block">
  475. for( animals_type::iterator i = animals.begin();
  476. i != animals.end(); ++i )
  477. {
  478. if( !boost::is_null(i) ) // always check for validity
  479. i-&gt;eat();
  480. }
  481. </pre>
  482. <p>If the container support random access, you may also check this as</p>
  483. <pre class="literal-block">
  484. for( animals_type::size_type i = 0u;
  485. i != animals.size(); ++i )
  486. {
  487. if( !animals.is_null(i) )
  488. animals[i].eat();
  489. }
  490. </pre>
  491. <p>Note that it is meaningless to insert
  492. null into <tt class="docutils literal">ptr_set</tt> and <tt class="docutils literal">ptr_multiset</tt>.</p>
  493. </div>
  494. <div class="section" id="cloneability">
  495. <h1>Cloneability</h1>
  496. <p>In OO programming it is typical to prohibit copying of objects; the
  497. objects may sometimes be allowed to be Cloneable; for example,:</p>
  498. <pre class="literal-block">
  499. animal* animal::clone() const
  500. {
  501. return do_clone(); // implemented by private virtual function
  502. }
  503. </pre>
  504. <p>If the OO hierarchy thus allows cloning, we need to tell the
  505. pointer containers how cloning is to be done. This is simply
  506. done by defining a free-standing function, <tt class="docutils literal">new_clone()</tt>,
  507. in the same namespace as
  508. the object hierarchy:</p>
  509. <pre class="literal-block">
  510. inline animal* new_clone( const animal&amp; a )
  511. {
  512. return a.clone();
  513. }
  514. </pre>
  515. <p>That is all, now a lot of functions in a pointer container
  516. can exploit the cloneability of the animal objects. For example</p>
  517. <pre class="literal-block">
  518. typedef boost::ptr_list&lt;animal&gt; zoo_type;
  519. zoo_type zoo, another_zoo;
  520. ...
  521. another_zoo.assign( zoo.begin(), zoo.end() );
  522. </pre>
  523. <p>will fill another zoo with clones of the first zoo. Similarly,
  524. <tt class="docutils literal">insert()</tt> can now insert clones into your pointer container</p>
  525. <pre class="literal-block">
  526. another_zoo.insert( another_zoo.begin(), zoo.begin(), zoo.end() );
  527. </pre>
  528. <p>The whole container can now also be cloned</p>
  529. <pre class="literal-block">
  530. zoo_type yet_another_zoo = zoo.clone();
  531. </pre>
  532. <p>Copying or assigning the container has the same effect as cloning (though it is slightly cheaper):</p>
  533. <pre class="literal-block">
  534. zoo_type yet_another_zoo = zoo;
  535. </pre>
  536. <p>Copying also support derived-to-base class conversions:</p>
  537. <pre class="literal-block">
  538. boost::ptr_vector&lt;monkey&gt; monkeys = boost::assign::ptr_list_of&lt;monkey&gt;( &quot;bobo&quot; )( &quot;bebe&quot;)( &quot;uhuh&quot; );
  539. boost::ptr_vector&lt;animal&gt; animals = monkeys;
  540. </pre>
  541. <p>This also works for maps:</p>
  542. <pre class="literal-block">
  543. boost::ptr_map&lt;std::string,monkey&gt; monkeys = ...;
  544. boost::ptr_map&lt;std::string,animal&gt; animals = monkeys;
  545. </pre>
  546. </div>
  547. <div class="section" id="new-functions">
  548. <h1>New functions</h1>
  549. <p>Given that we know we are working with pointers, a few new functions
  550. make sense. For example, say you want to remove an
  551. animal from the zoo</p>
  552. <pre class="literal-block">
  553. zoo_type::auto_type the_animal = zoo.release( zoo.begin() );
  554. the_animal-&gt;eat();
  555. animal* the_animal_ptr = the_animal.release(); // now this is not deleted
  556. zoo.release(2); // for random access containers
  557. </pre>
  558. <p>You can think of <tt class="docutils literal">auto_type</tt> as a non-copyable form of
  559. <tt class="docutils literal"><span class="pre">std::auto_ptr</span></tt>. Notice that when you release an object, the
  560. pointer is removed from the container and the containers size
  561. shrinks. For containers that store nulls, we can exploit that
  562. <tt class="docutils literal">auto_type</tt> is convertible to <tt class="docutils literal">bool</tt>:</p>
  563. <pre class="literal-block">
  564. if( ptr_vector&lt; nullable&lt;T&gt; &gt;::auto_type r = vec.pop_back() )
  565. {
  566. ...
  567. }
  568. </pre>
  569. <p>You can also release the entire container if you
  570. want to return it from a function</p>
  571. <pre class="literal-block">
  572. <a class="reference external" href="compatible_smart_ptr.html"><em>compatible-smart-ptr</em></a>&lt; boost::ptr_deque&lt;animal&gt; &gt; get_zoo()
  573. {
  574. boost::ptr_deque&lt;animal&gt; result;
  575. ...
  576. return result.release(); // give up ownership
  577. }
  578. ...
  579. boost::ptr_deque&lt;animal&gt; animals = get_zoo();
  580. </pre>
  581. <p>Let us assume we want to move an animal object from
  582. one zoo to another. In other words, we want to move the
  583. animal and the responsibility of it to another zoo</p>
  584. <pre class="literal-block">
  585. another_zoo.transfer( another_zoo.end(), // insert before end
  586. zoo.begin(), // insert this animal ...
  587. zoo ); // from this container
  588. </pre>
  589. <p>This kind of &quot;move-semantics&quot; is different from
  590. normal value-based containers. You can think of <tt class="docutils literal">transfer()</tt>
  591. as the same as <tt class="docutils literal">splice()</tt> on <tt class="docutils literal"><span class="pre">std::list</span></tt>.</p>
  592. <p>If you want to replace an element, you can easily do so</p>
  593. <pre class="literal-block">
  594. zoo_type::auto_type old_animal = zoo.replace( zoo.begin(), new monkey(&quot;bibi&quot;) );
  595. zoo.replace( 2, old_animal.release() ); // for random access containers
  596. </pre>
  597. <p>A map is slightly different to iterate over than standard maps.
  598. Now we say</p>
  599. <pre class="literal-block">
  600. typedef boost::ptr_map&lt;std::string, boost::nullable&lt;animal&gt; &gt; animal_map;
  601. animal_map map;
  602. ...
  603. for( animal_map::const_iterator i = map.begin(), e = map.end(); i != e; ++i )
  604. {
  605. std::cout &lt;&lt; &quot;\n key: &quot; &lt;&lt; i-&gt;first;
  606. std::cout &lt;&lt; &quot;\n age: &quot;;
  607. if( boost::is_null(i) )
  608. std::cout &lt;&lt; &quot;unknown&quot;;
  609. else
  610. std::cout &lt;&lt; i-&gt;second-&gt;age();
  611. }
  612. </pre>
  613. <p>Except for the check for null, this looks like it would with a normal map. But if <tt class="docutils literal">age()</tt> had
  614. not been a <tt class="docutils literal">const</tt> member function,
  615. it would not have compiled.</p>
  616. <p>Maps can also be indexed with bounds-checking</p>
  617. <pre class="literal-block">
  618. try
  619. {
  620. animal&amp; bobo = map.at(&quot;bobo&quot;);
  621. }
  622. catch( boost::bad_ptr_container_operation&amp; e )
  623. {
  624. // &quot;bobo&quot; not found
  625. }
  626. </pre>
  627. </div>
  628. <div class="section" id="compatible-smart-pointer-overloads">
  629. <h1>Compatible smart pointer overloads</h1>
  630. <p>Every time there is a function that takes a <tt class="docutils literal">T*</tt> parameter, there is
  631. also a function overload (or two) taking a <tt class="docutils literal"><span class="pre"><a class="reference external" href="compatible_smart_ptr.html"><em>compatible-smart-ptr</em></a>&lt;U&gt;</span></tt>
  632. parameter. This is of course done to make the library intregrate
  633. seamlessly with <tt class="docutils literal"><span class="pre">std::auto_ptr</span></tt> or <tt class="docutils literal"><span class="pre">std::unique_ptr</span></tt>. For example,
  634. consider a statement like</p>
  635. <pre class="literal-block">
  636. std::ptr_vector&lt;Base&gt; vec;
  637. vec.push_back( new Base );
  638. </pre>
  639. <p>If the compiler supports <tt class="docutils literal"><span class="pre">std::auto_ptr</span></tt>, this is complemented
  640. by</p>
  641. <pre class="literal-block">
  642. std::auto_ptr&lt;Derived&gt; p( new Derived );
  643. vec.push_back( p );
  644. </pre>
  645. <p>Similarly if <tt class="docutils literal"><span class="pre">std::unique_ptr</span></tt> is available, we can write</p>
  646. <pre class="literal-block">
  647. std::unique_ptr&lt;Derived&gt; p( new Derived );
  648. vec.push_back( std::move( p ) );
  649. </pre>
  650. <p>Notice that the template argument for <tt class="docutils literal"><span class="pre"><a class="reference external" href="compatible_smart_ptr.html"><em>compatible-smart-ptr</em></a></span></tt> does not need to
  651. follow the template argument for <tt class="docutils literal">ptr_vector</tt> as long as <tt class="docutils literal">Derived*</tt>
  652. can be implicitly converted to <tt class="docutils literal">Base*</tt>.</p>
  653. </div>
  654. <div class="section" id="algorithms">
  655. <h1>Algorithms</h1>
  656. <p>Unfortunately it is not possible to use pointer containers with
  657. mutating algorithms from the standard library. However,
  658. the most useful ones
  659. are instead provided as member functions:</p>
  660. <pre class="literal-block">
  661. boost::ptr_vector&lt;animal&gt; zoo;
  662. ...
  663. zoo.sort(); // assume 'bool operator&lt;( const animal&amp;, const animal&amp; )'
  664. zoo.sort( std::less&lt;animal&gt;() ); // the same, notice no '*' is present
  665. zoo.sort( zoo.begin(), zoo.begin() + 5 ); // sort selected range
  666. </pre>
  667. <p>Notice that predicates are automatically wrapped in an <a class="reference external" href="indirect_fun.html">indirect_fun</a> object.</p>
  668. <p>You can remove equal and adjacent elements using <tt class="docutils literal">unique()</tt>:</p>
  669. <pre class="literal-block">
  670. zoo.unique(); // assume 'bool operator==( const animal&amp;, const animal&amp; )'
  671. zoo.unique( zoo.begin(), zoo.begin() + 5, my_comparison_predicate() );
  672. </pre>
  673. <p>If you just want to remove certain elements, use <tt class="docutils literal">erase_if</tt>:</p>
  674. <pre class="literal-block">
  675. zoo.erase_if( my_predicate() );
  676. </pre>
  677. <p>Finally you may want to merge two sorted containers:</p>
  678. <pre class="literal-block">
  679. boost::ptr_vector&lt;animal&gt; another_zoo = ...;
  680. another_zoo.sort(); // sorted wrt. to same order as 'zoo'
  681. zoo.merge( another_zoo );
  682. BOOST_ASSERT( another_zoo.empty() );
  683. </pre>
  684. <p>That is all; now you have learned all the basics!</p>
  685. <hr><p><strong>See also</strong></p>
  686. <ul class="simple">
  687. <li><a class="reference external" href="guidelines.html">Usage guidelines</a></li>
  688. <li><a class="reference external" href="../../conversion/cast.htm#Polymorphic_castl">Cast utilities</a></li>
  689. </ul>
  690. <p><strong>Navigate</strong></p>
  691. <ul class="simple">
  692. <li><a class="reference external" href="ptr_container.html">home</a></li>
  693. <li><a class="reference external" href="examples.html">examples</a></li>
  694. </ul>
  695. <hr><table class="docutils field-list" frame="void" rules="none">
  696. <col class="field-name" />
  697. <col class="field-body" />
  698. <tbody valign="top">
  699. <tr class="field"><th class="field-name">Copyright:</th><td class="field-body">Thorsten Ottosen 2004-2006. Use, modification and distribution is subject to the Boost Software License, Version 1.0 (see <a class="reference external" href="http://www.boost.org/LICENSE_1_0.txt">LICENSE_1_0.txt</a>).</td>
  700. </tr>
  701. </tbody>
  702. </table>
  703. </div>
  704. </div>
  705. </body>
  706. </html>