1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"> 2 3<html> 4<head> 5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 6<title>Boost.Assignment Documentation </title> 7<link rel="stylesheet" href="style.css" type="text/css"> 8</head> 9 10<body> 11<pre> 12<span class=comment>// Boost.Assign library 13// 14// Copyright Thorsten Ottosen 2003-2004. Use, modification and 15// distribution is subject to the Boost Software License, Version 16// 1.0. (See accompanying file LICENSE_1_0.txt or copy at 17// http://www.boost.org/LICENSE_1_0.txt) 18// 19// For more information, see http://www.boost.org/libs/assign/ 20// 21 22</span> 23<span class=preprocessor>#include </span><span class=special><</span><span class=identifier>boost</span><span class=special>/</span><span class=identifier>detail</span><span class=special>/</span><span class=identifier>workaround</span><span class=special>.</span><span class=identifier>hpp</span><span class=special>> 24</span> 25<span class=preprocessor>#if </span><span class=identifier>BOOST_WORKAROUND</span><span class=special>(</span><span class=identifier>__BORLANDC__</span><span class=special>, </span><span class=identifier>BOOST_TESTED_AT</span><span class=special>(</span><span class=number>0</span><span class=identifier>x564</span><span class=special>))</span> 26###<span class=identifier>pragma </span><span class=identifier>warn </span><span class=special>-</span><span class=number>8091 </span><span class=comment>// suppress warning in Boost.Test</span> 27###<span class=identifier>pragma </span><span class=identifier>warn </span><span class=special>-</span><span class=number>8057 </span><span class=comment>// unused argument argc/argv in Boost.Test</span> 28<span class=preprocessor>#endif 29</span> 30<span class=preprocessor>#include </span><span class=special><</span><span class=identifier>boost</span><span class=special>/</span><span class=identifier>assign</span><span class=special>/</span><span class=identifier>list_of</span><span class=special>.</span><span class=identifier>hpp</span><span class=special>></span> 31<span class=preprocessor>#include </span><span class=special><</span><span class=identifier>boost</span><span class=special>/</span><span class=identifier>assign</span><span class=special>/</span><span class=identifier>list_inserter</span><span class=special>.</span><span class=identifier>hpp</span><span class=special>></span> 32<span class=preprocessor>#include </span><span class=special><</span><span class=identifier>boost</span><span class=special>/</span><span class=identifier>multi_index_container</span><span class=special>.</span><span class=identifier>hpp</span><span class=special>></span> 33<span class=preprocessor>#include </span><span class=special><</span><span class=identifier>boost</span><span class=special>/</span><span class=identifier>multi_index</span><span class=special>/</span><span class=identifier>identity</span><span class=special>.</span><span class=identifier>hpp</span><span class=special>></span> 34<span class=preprocessor>#include </span><span class=special><</span><span class=identifier>boost</span><span class=special>/</span><span class=identifier>multi_index</span><span class=special>/</span><span class=identifier>member</span><span class=special>.</span><span class=identifier>hpp</span><span class=special>></span> 35<span class=preprocessor>#include </span><span class=special><</span><span class=identifier>boost</span><span class=special>/</span><span class=identifier>multi_index</span><span class=special>/</span><span class=identifier>ordered_index</span><span class=special>.</span><span class=identifier>hpp</span><span class=special>></span> 36<span class=preprocessor>#include </span><span class=special><</span><span class=identifier>boost</span><span class=special>/</span><span class=identifier>multi_index</span><span class=special>/</span><span class=identifier>sequenced_index</span><span class=special>.</span><span class=identifier>hpp</span><span class=special>></span> 37<span class=preprocessor>#include </span><span class=special><</span><span class=identifier>boost</span><span class=special>/</span><span class=identifier>test</span><span class=special>/</span><span class=identifier>unit_test</span><span class=special>.</span><span class=identifier>hpp</span><span class=special>></span> 38<span class=preprocessor>#include </span><span class=special><</span><span class=identifier>boost</span><span class=special>/</span><span class=identifier>test</span><span class=special>/</span><span class=identifier>test_tools</span><span class=special>.</span><span class=identifier>hpp</span><span class=special>></span> 39<span class=preprocessor>#include </span><span class=special><</span><span class=identifier>cstddef</span><span class=special>></span> 40<span class=preprocessor>#include </span><span class=special><</span><span class=identifier>ostream</span><span class=special>></span> 41<span class=preprocessor>#include </span><span class=special><</span><span class=identifier>string</span><span class=special>> 42</span> 43<span class=keyword>using </span><span class=keyword>namespace </span><span class=identifier>boost</span><span class=special>;</span> 44<span class=keyword>using </span><span class=keyword>namespace </span><span class=identifier>boost</span><span class=special>::</span><span class=identifier>multi_index</span><span class=special>;</span> 45<span class=keyword>namespace </span><span class=identifier>ba </span><span class=special>= </span><span class=identifier>boost</span><span class=special>::</span><span class=identifier>assign</span><span class=special>; 46</span> 47<span class=comment>// 48// Define a classical multi_index_container for employees 49//</span> 50<span class=keyword>struct </span><span class=identifier>employee</span> 51<span class=special>{ 52 </span><span class=keyword>int </span><span class=identifier>id</span><span class=special>; 53 </span><span class=identifier>std</span><span class=special>::</span><span class=identifier>string </span><span class=identifier>name</span><span class=special>; 54 </span><span class=keyword>int </span><span class=identifier>age</span><span class=special>; 55 56 </span><span class=identifier>employee</span><span class=special>(</span><span class=keyword>int </span><span class=identifier>id_</span><span class=special>,</span><span class=identifier>std</span><span class=special>::</span><span class=identifier>string </span><span class=identifier>name_</span><span class=special>,</span><span class=keyword>int </span><span class=identifier>age_</span><span class=special>):</span><span class=identifier>id</span><span class=special>(</span><span class=identifier>id_</span><span class=special>),</span><span class=identifier>name</span><span class=special>(</span><span class=identifier>name_</span><span class=special>),</span><span class=identifier>age</span><span class=special>(</span><span class=identifier>age_</span><span class=special>){} 57 58 </span><span class=keyword>bool </span><span class=keyword>operator</span><span class=special>==(</span><span class=keyword>const </span><span class=identifier>employee</span><span class=special>& </span><span class=identifier>x</span><span class=special>)</span><span class=keyword>const 59 </span><span class=special>{ 60 </span><span class=keyword>return </span><span class=identifier>id</span><span class=special>==</span><span class=identifier>x</span><span class=special>.</span><span class=identifier>id</span><span class=special>&&</span><span class=identifier>name</span><span class=special>==</span><span class=identifier>x</span><span class=special>.</span><span class=identifier>name</span><span class=special>&&</span><span class=identifier>age</span><span class=special>==</span><span class=identifier>x</span><span class=special>.</span><span class=identifier>age</span><span class=special>; 61 </span><span class=special>} 62 63 </span><span class=keyword>bool </span><span class=keyword>operator</span><span class=special><(</span><span class=keyword>const </span><span class=identifier>employee</span><span class=special>& </span><span class=identifier>x</span><span class=special>)</span><span class=keyword>const 64 </span><span class=special>{ 65 </span><span class=keyword>return </span><span class=identifier>id</span><span class=special><</span><span class=identifier>x</span><span class=special>.</span><span class=identifier>id</span><span class=special>; 66 </span><span class=special>} 67 68 </span><span class=keyword>bool </span><span class=keyword>operator</span><span class=special>!=(</span><span class=keyword>const </span><span class=identifier>employee</span><span class=special>& </span><span class=identifier>x</span><span class=special>)</span><span class=keyword>const</span><span class=special>{</span><span class=keyword>return </span><span class=special>!(*</span><span class=keyword>this</span><span class=special>==</span><span class=identifier>x</span><span class=special>);} 69 </span><span class=keyword>bool </span><span class=keyword>operator</span><span class=special>> </span><span class=special>(</span><span class=keyword>const </span><span class=identifier>employee</span><span class=special>& </span><span class=identifier>x</span><span class=special>)</span><span class=keyword>const</span><span class=special>{</span><span class=keyword>return </span><span class=identifier>x</span><span class=special><*</span><span class=keyword>this</span><span class=special>;} 70 </span><span class=keyword>bool </span><span class=keyword>operator</span><span class=special>>=(</span><span class=keyword>const </span><span class=identifier>employee</span><span class=special>& </span><span class=identifier>x</span><span class=special>)</span><span class=keyword>const</span><span class=special>{</span><span class=keyword>return </span><span class=special>!(*</span><span class=keyword>this</span><span class=special><</span><span class=identifier>x</span><span class=special>);} 71 </span><span class=keyword>bool </span><span class=keyword>operator</span><span class=special><=(</span><span class=keyword>const </span><span class=identifier>employee</span><span class=special>& </span><span class=identifier>x</span><span class=special>)</span><span class=keyword>const</span><span class=special>{</span><span class=keyword>return </span><span class=special>!(</span><span class=identifier>x</span><span class=special><*</span><span class=keyword>this</span><span class=special>);} 72 73 </span><span class=keyword>struct </span><span class=identifier>comp_id 74 </span><span class=special>{ 75 </span><span class=keyword>bool </span><span class=keyword>operator</span><span class=special>()(</span><span class=keyword>int </span><span class=identifier>x</span><span class=special>,</span><span class=keyword>const </span><span class=identifier>employee</span><span class=special>& </span><span class=identifier>e2</span><span class=special>)</span><span class=keyword>const</span><span class=special>{</span><span class=keyword>return </span><span class=identifier>x</span><span class=special><</span><span class=identifier>e2</span><span class=special>.</span><span class=identifier>id</span><span class=special>;} 76 </span><span class=keyword>bool </span><span class=keyword>operator</span><span class=special>()(</span><span class=keyword>const </span><span class=identifier>employee</span><span class=special>& </span><span class=identifier>e1</span><span class=special>,</span><span class=keyword>int </span><span class=identifier>x</span><span class=special>)</span><span class=keyword>const</span><span class=special>{</span><span class=keyword>return </span><span class=identifier>e1</span><span class=special>.</span><span class=identifier>id</span><span class=special><</span><span class=identifier>x</span><span class=special>;} 77 </span><span class=special>}; 78 79 </span><span class=keyword>friend </span><span class=identifier>std</span><span class=special>::</span><span class=identifier>ostream</span><span class=special>& </span><span class=keyword>operator</span><span class=special><<(</span><span class=identifier>std</span><span class=special>::</span><span class=identifier>ostream</span><span class=special>& </span><span class=identifier>os</span><span class=special>,</span><span class=keyword>const </span><span class=identifier>employee</span><span class=special>& </span><span class=identifier>e</span><span class=special>) 80 </span><span class=special>{ 81 </span><span class=identifier>os</span><span class=special><<</span><span class=identifier>e</span><span class=special>.</span><span class=identifier>id</span><span class=special><<</span><span class=string>" "</span><span class=special><<</span><span class=identifier>e</span><span class=special>.</span><span class=identifier>name</span><span class=special><<</span><span class=string>" "</span><span class=special><<</span><span class=identifier>e</span><span class=special>.</span><span class=identifier>age</span><span class=special><<</span><span class=identifier>std</span><span class=special>::</span><span class=identifier>endl</span><span class=special>; 82 </span><span class=keyword>return </span><span class=identifier>os</span><span class=special>; 83 </span><span class=special>}</span> 84<span class=special>}; 85</span> 86<span class=keyword>struct </span><span class=identifier>name</span><span class=special>{};</span> 87<span class=keyword>struct </span><span class=identifier>by_name</span><span class=special>{};</span> 88<span class=keyword>struct </span><span class=identifier>age</span><span class=special>{};</span> 89<span class=keyword>struct </span><span class=identifier>as_inserted</span><span class=special>{}; 90</span> 91<span class=keyword>typedef 92 </span><span class=identifier>multi_index_container</span><span class=special>< 93 </span><span class=identifier>employee</span><span class=special>, 94 </span><span class=identifier>indexed_by</span><span class=special>< 95 </span><span class=identifier>ordered_unique</span><span class=special>< 96 </span><span class=identifier>identity</span><span class=special><</span><span class=identifier>employee</span><span class=special>> </span><span class=special>>, 97 </span><span class=identifier>ordered_non_unique</span><span class=special>< 98 </span><span class=identifier>tag</span><span class=special><</span><span class=identifier>name</span><span class=special>,</span><span class=identifier>by_name</span><span class=special>>, 99 </span><span class=identifier>BOOST_MULTI_INDEX_MEMBER</span><span class=special>(</span><span class=identifier>employee</span><span class=special>,</span><span class=identifier>std</span><span class=special>::</span><span class=identifier>string</span><span class=special>,</span><span class=identifier>name</span><span class=special>)>, 100 </span><span class=identifier>ordered_non_unique</span><span class=special>< 101 </span><span class=identifier>tag</span><span class=special><</span><span class=identifier>age</span><span class=special>>, 102 </span><span class=identifier>BOOST_MULTI_INDEX_MEMBER</span><span class=special>(</span><span class=identifier>employee</span><span class=special>,</span><span class=keyword>int</span><span class=special>,</span><span class=identifier>age</span><span class=special>)>, 103 </span><span class=identifier>sequenced</span><span class=special>< 104 </span><span class=identifier>tag</span><span class=special><</span><span class=identifier>as_inserted</span><span class=special>> </span><span class=special>> </span><span class=special>> </span><span class=special>> 105 </span><span class=identifier>employee_set</span><span class=special>; 106</span> 107<span class=preprocessor>#if </span><span class=identifier>defined</span><span class=special>(</span><span class=identifier>BOOST_NO_MEMBER_TEMPLATES</span><span class=special>)</span> 108<span class=keyword>typedef </span><span class=identifier>nth_index</span><span class=special>< 109 </span><span class=identifier>employee_set</span><span class=special>,</span><span class=number>1</span><span class=special>>::</span><span class=identifier>type </span><span class=identifier>employee_set_by_name</span><span class=special>;</span> 110<span class=preprocessor>#else</span> 111<span class=keyword>typedef </span><span class=identifier>employee_set</span><span class=special>::</span><span class=identifier>nth_index</span><span class=special><</span><span class=number>1</span><span class=special>>::</span><span class=identifier>type </span><span class=identifier>employee_set_by_name</span><span class=special>;</span> 112<span class=preprocessor>#endif 113</span> 114<span class=keyword>typedef </span><span class=identifier>boost</span><span class=special>::</span><span class=identifier>multi_index</span><span class=special>::</span><span class=identifier>index</span><span class=special>< 115 </span><span class=identifier>employee_set</span><span class=special>,</span><span class=identifier>age</span><span class=special>>::</span><span class=identifier>type </span><span class=identifier>employee_set_by_age</span><span class=special>;</span> 116<span class=keyword>typedef </span><span class=identifier>boost</span><span class=special>::</span><span class=identifier>multi_index</span><span class=special>::</span><span class=identifier>index</span><span class=special>< 117 </span><span class=identifier>employee_set</span><span class=special>,</span><span class=identifier>as_inserted</span><span class=special>>::</span><span class=identifier>type </span><span class=identifier>employee_set_as_inserted</span><span class=special>; 118</span> 119<span class=comment>// 120// Define a multi_index_container with a list-like index and an ordered index 121//</span> 122<span class=keyword>typedef </span><span class=identifier>multi_index_container</span><span class=special>< 123 </span><span class=identifier>std</span><span class=special>::</span><span class=identifier>string</span><span class=special>, 124 </span><span class=identifier>indexed_by</span><span class=special>< 125 </span><span class=identifier>sequenced</span><span class=special><>, </span><span class=comment>// list-like index 126 </span><span class=identifier>ordered_non_unique</span><span class=special><</span><span class=identifier>identity</span><span class=special><</span><span class=identifier>std</span><span class=special>::</span><span class=identifier>string</span><span class=special>> </span><span class=special>> </span><span class=comment>// words by alphabetical order 127 </span><span class=special>></span> 128<span class=special>> </span><span class=identifier>text_container</span><span class=special>; 129 130 131</span> 132<span class=keyword>void </span><span class=identifier>test_multi_index_container</span><span class=special>()</span> 133<span class=special>{ 134 </span><span class=identifier>employee_set </span><span class=identifier>eset </span><span class=special>= </span><span class=identifier>ba</span><span class=special>::</span><span class=identifier>list_of</span><span class=special>< </span><span class=identifier>employee </span><span class=special>>(</span><span class=number>1</span><span class=special>,</span><span class=string>"Franz"</span><span class=special>,</span><span class=number>30</span><span class=special>)(</span><span class=number>2</span><span class=special>,</span><span class=string>"Hanz"</span><span class=special>,</span><span class=number>40</span><span class=special>)(</span><span class=number>3</span><span class=special>,</span><span class=string>"Ilse"</span><span class=special>,</span><span class=number>50</span><span class=special>); 135 </span><span class=identifier>BOOST_CHECK</span><span class=special>( </span><span class=identifier>eset</span><span class=special>.</span><span class=identifier>size</span><span class=special>() </span><span class=special>== </span><span class=number>3u </span><span class=special>); 136 137 </span><span class=comment>// 138 // This container is associative, hence we can use 'insert()' 139 // 140 141 </span><span class=identifier>ba</span><span class=special>::</span><span class=identifier>insert</span><span class=special>( </span><span class=identifier>eset </span><span class=special>)(</span><span class=number>4</span><span class=special>,</span><span class=string>"Kurt"</span><span class=special>,</span><span class=number>55</span><span class=special>)(</span><span class=number>5</span><span class=special>,</span><span class=string>"Bjarne"</span><span class=special>,</span><span class=number>77</span><span class=special>)(</span><span class=number>7</span><span class=special>,</span><span class=string>"Thorsten"</span><span class=special>,</span><span class=number>24</span><span class=special>); 142 </span><span class=identifier>BOOST_CHECK</span><span class=special>( </span><span class=identifier>eset</span><span class=special>.</span><span class=identifier>size</span><span class=special>() </span><span class=special>== </span><span class=number>6u </span><span class=special>); 143 144 </span><span class=identifier>employee_set_by_name</span><span class=special>& </span><span class=identifier>name_index </span><span class=special>= </span><span class=identifier>boost</span><span class=special>::</span><span class=identifier>multi_index</span><span class=special>::</span><span class=identifier>get</span><span class=special><</span><span class=identifier>name</span><span class=special>>(</span><span class=identifier>eset</span><span class=special>); 145 </span><span class=identifier>employee_set_by_name</span><span class=special>::</span><span class=identifier>iterator </span><span class=identifier>i </span><span class=special>= </span><span class=identifier>name_index</span><span class=special>.</span><span class=identifier>find</span><span class=special>(</span><span class=string>"Ilse"</span><span class=special>); 146 </span><span class=identifier>BOOST_CHECK</span><span class=special>( </span><span class=identifier>i</span><span class=special>-></span><span class=identifier>id </span><span class=special>== </span><span class=number>3 </span><span class=special>); 147 </span><span class=identifier>BOOST_CHECK</span><span class=special>( </span><span class=identifier>i</span><span class=special>-></span><span class=identifier>age </span><span class=special>== </span><span class=number>50 </span><span class=special>); 148 149 </span><span class=identifier>text_container </span><span class=identifier>text </span><span class=special>= </span><span class=identifier>ba</span><span class=special>::</span><span class=identifier>list_of</span><span class=special>< </span><span class=identifier>std</span><span class=special>::</span><span class=identifier>string </span><span class=special>>(</span><span class=string>"Have"</span><span class=special>)(</span><span class=string>"you"</span><span class=special>)(</span><span class=string>"ever"</span><span class=special>)(</span><span class=string>"wondered"</span><span class=special>)(</span><span class=string>"how"</span><span class=special>)(</span><span class=string>"much"</span><span class=special>)(</span><span class=string>"Boost"</span><span class=special>)(</span><span class=string>"rocks?!"</span><span class=special>); 150 </span><span class=identifier>BOOST_CHECK_EQUAL</span><span class=special>( </span><span class=identifier>text</span><span class=special>.</span><span class=identifier>size</span><span class=special>(), </span><span class=number>8u </span><span class=special>); 151 </span><span class=identifier>BOOST_CHECK_EQUAL</span><span class=special>( </span><span class=special>*</span><span class=identifier>text</span><span class=special>.</span><span class=identifier>begin</span><span class=special>(), </span><span class=string>"Have" </span><span class=special>); 152 153 </span><span class=comment>// 154 // This container is a sequence, hence we can use 'push_back()' and 'push_font()' 155 // 156 157 </span><span class=identifier>ba</span><span class=special>::</span><span class=identifier>push_back</span><span class=special>( </span><span class=identifier>text </span><span class=special>)(</span><span class=string>"Well"</span><span class=special>)(</span><span class=string>","</span><span class=special>)(</span><span class=string>"A"</span><span class=special>)(</span><span class=string>"LOT"</span><span class=special>)(</span><span class=string>","</span><span class=special>)(</span><span class=string>"obviously!"</span><span class=special>); 158 </span><span class=identifier>BOOST_CHECK_EQUAL</span><span class=special>( </span><span class=identifier>text</span><span class=special>.</span><span class=identifier>size</span><span class=special>(), </span><span class=number>14u </span><span class=special>); 159 </span><span class=identifier>BOOST_CHECK_EQUAL</span><span class=special>( </span><span class=special>*--</span><span class=identifier>text</span><span class=special>.</span><span class=identifier>end</span><span class=special>(), </span><span class=string>"obviously!" </span><span class=special>); 160 161 </span><span class=identifier>ba</span><span class=special>::</span><span class=identifier>push_front</span><span class=special>( </span><span class=identifier>text </span><span class=special>) </span><span class=special>= </span><span class=string>"question:"</span><span class=special>, </span><span class=string>"simple"</span><span class=special>, </span><span class=string>"A"</span><span class=special>; 162 </span><span class=identifier>BOOST_CHECK_EQUAL</span><span class=special>( </span><span class=identifier>text</span><span class=special>.</span><span class=identifier>size</span><span class=special>(), </span><span class=number>17u </span><span class=special>); 163 </span><span class=identifier>BOOST_CHECK_EQUAL</span><span class=special>( </span><span class=identifier>text</span><span class=special>.</span><span class=identifier>front</span><span class=special>(), </span><span class=string>"A" </span><span class=special>);</span> 164<span class=special>} 165 166 167</span> 168<span class=preprocessor>#include </span><span class=special><</span><span class=identifier>boost</span><span class=special>/</span><span class=identifier>test</span><span class=special>/</span><span class=identifier>included</span><span class=special>/</span><span class=identifier>unit_test_framework</span><span class=special>.</span><span class=identifier>hpp</span><span class=special>> 169</span> 170<span class=keyword>using </span><span class=identifier>boost</span><span class=special>::</span><span class=identifier>unit_test_framework</span><span class=special>::</span><span class=identifier>test_suite</span><span class=special>; 171</span> 172<span class=identifier>test_suite</span><span class=special>* </span><span class=identifier>init_unit_test_suite</span><span class=special>( </span><span class=keyword>int </span><span class=identifier>argc</span><span class=special>, </span><span class=keyword>char</span><span class=special>* </span><span class=identifier>argv</span><span class=special>[] </span><span class=special>)</span> 173<span class=special>{ 174 </span><span class=identifier>test_suite</span><span class=special>* </span><span class=identifier>test </span><span class=special>= </span><span class=identifier>BOOST_TEST_SUITE</span><span class=special>( </span><span class=string>"List Test Suite" </span><span class=special>); 175 176 </span><span class=identifier>test</span><span class=special>-></span><span class=identifier>add</span><span class=special>( </span><span class=identifier>BOOST_TEST_CASE</span><span class=special>( </span><span class=special>&</span><span class=identifier>test_multi_index_container </span><span class=special>) </span><span class=special>); 177 178 </span><span class=keyword>return </span><span class=identifier>test</span><span class=special>;</span> 179<span class=special>} 180 181 182</span> 183</pre> 184</body> 185</html> 186