<div class="output_wrapper" id="output_wrapper_id" style="font-size: 17px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 1px; letter-spacing: 1px; font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;"><blockquote style="line-height: inherit; display: block; padding: 15px 15px 15px 1rem; font-size: 0.9em; margin: 1em 0px; color: rgb(129, 145, 152); border-left: 6px solid rgb(220, 230, 240); background: rgb(242, 247, 251); overflow: auto; overflow-wrap: normal; word-break: normal;"> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 0px;">特別聲明:<br>本文翻譯自 Modern C++ Programming Cookbook 一書中的「Learning Modern Core Language Features」章節中的「Using range-based for loops to iterate on a range」小節。<br>翻譯此文純粹做爲學習之用,版權歸原做者及出版社全部。</p> </blockquote> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">許多編程語言支持 for 循環的稱之爲 for each 的變體,即針對集合中的元素重複一組語句。在 C++11 以前 C++沒有相應的核心語言支持。最接近的特性是來自標準庫中的被稱爲 std::for_each 的通用算法,它在範圍的全部元素上應用一個函數。C++11 帶來了對 for each 的語言級支持,實際稱之爲基於範圍 for 循環。新的 C++17 標準對原始語言特性提供了一些改進。</p> <h3 id="hgettingready" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid rgb(0, 172, 193);"><span style="font-size: inherit; line-height: inherit; margin: 0px; display: inline-block; font-weight: normal; background: rgb(0, 172, 193); color: rgb(255, 255, 255); padding: 3px 10px 0px; border-top-right-radius: 3px; border-top-left-radius: 4px; margin-right: 2px;">Getting ready</span></h3> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">在 C++11 中,基於範圍 for 循環的通用語法以下:</p> <pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="cpp language-cpp hljs" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; background: rgb(29, 31, 33); color: rgb(197, 200, 198); padding: 0.5em; display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">1</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(178, 148, 187); word-wrap: inherit !important; word-break: inherit !important;">for</span> (range_declaration : range_expression) loop_statement<br></code></pre> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">爲了舉例說明基於範圍的 for 循環的不一樣用法,咱們將使用下面這些函數返回元素序列:</p> <pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="cpp language-cpp hljs" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; background: rgb(29, 31, 33); color: rgb(197, 200, 198); padding: 0.5em; display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 1</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">std</span>::<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">vector</span><<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(178, 148, 187); word-wrap: inherit !important; word-break: inherit !important;">int</span>> getRates()<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 2</span>{<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 3</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(178, 148, 187); word-wrap: inherit !important; word-break: inherit !important;">return</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">std</span>::<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">vector</span><<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(178, 148, 187); word-wrap: inherit !important; word-break: inherit !important;">int</span>>{<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">1</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">1</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">2</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">3</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">5</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">8</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">13</span>};<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 4</span>}<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 5</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 6</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">std</span>::<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">multimap</span><<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(178, 148, 187); word-wrap: inherit !important; word-break: inherit !important;">int</span>, <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(178, 148, 187); word-wrap: inherit !important; word-break: inherit !important;">bool</span>> getRates2()<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 7</span>{<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 8</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(178, 148, 187); word-wrap: inherit !important; word-break: inherit !important;">return</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">std</span>::<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">multimap</span><<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(178, 148, 187); word-wrap: inherit !important; word-break: inherit !important;">int</span>, <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(178, 148, 187); word-wrap: inherit !important; word-break: inherit !important;">bool</span>>{<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 9</span> {<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">1</span>, <span class="hljs-literal" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">true</span>},<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">10</span> {<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">1</span>, <span class="hljs-literal" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">true</span>},<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">11</span> {<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">2</span>, <span class="hljs-literal" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">false</span>},<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">12</span> {<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">3</span>, <span class="hljs-literal" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">true</span>},<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">13</span> {<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">5</span>, <span class="hljs-literal" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">true</span>},<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">14</span> {<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">8</span>, <span class="hljs-literal" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">false</span>},<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">15</span> {<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">13</span>, <span class="hljs-literal" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">true</span>}<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">16</span> };<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">17</span>}<br></code></pre> <h3 id="hhowtodoit" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid rgb(0, 172, 193);"><span style="font-size: inherit; line-height: inherit; margin: 0px; display: inline-block; font-weight: normal; background: rgb(0, 172, 193); color: rgb(255, 255, 255); padding: 3px 10px 0px; border-top-right-radius: 3px; border-top-left-radius: 4px; margin-right: 2px;">How to do it…</span></h3> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">基於範圍的 for 循環能夠經過不一樣的方式使用:</p> <ul style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; padding-left: 32px; list-style-type: disc;"> <li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">爲序列元素提供一個特定類型:</span></li> </ul> <pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="cpp language-cpp hljs" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; background: rgb(29, 31, 33); color: rgb(197, 200, 198); padding: 0.5em; display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">1</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(178, 148, 187); word-wrap: inherit !important; word-break: inherit !important;">auto</span> rates = getRates();<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">2</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(178, 148, 187); word-wrap: inherit !important; word-break: inherit !important;">for</span> (<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(178, 148, 187); word-wrap: inherit !important; word-break: inherit !important;">int</span> rate : rates)<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">3</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">std</span>::<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">cout</span> << rate << <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">std</span>::<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">endl</span>;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">4</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">5</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(178, 148, 187); word-wrap: inherit !important; word-break: inherit !important;">for</span> (<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(178, 148, 187); word-wrap: inherit !important; word-break: inherit !important;">int</span> &rate : rates)<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">6</span> rate *= <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">2</span>;<br></code></pre> <ul style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; padding-left: 32px; list-style-type: disc;"> <li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">不指定類型並由編譯器進行推導:</span></li> </ul> <pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="cpp language-cpp hljs" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; background: rgb(29, 31, 33); color: rgb(197, 200, 198); padding: 0.5em; display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">1</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(178, 148, 187); word-wrap: inherit !important; word-break: inherit !important;">for</span> (<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(178, 148, 187); word-wrap: inherit !important; word-break: inherit !important;">auto</span> &&rate : getRates())<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">2</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">std</span>::<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">cout</span> << rate << <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">std</span>::<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">endl</span>;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">3</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">4</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(178, 148, 187); word-wrap: inherit !important; word-break: inherit !important;">for</span> (<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(178, 148, 187); word-wrap: inherit !important; word-break: inherit !important;">auto</span> &rate : rates)<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">5</span> rate *= <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">2</span>;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">6</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">7</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(178, 148, 187); word-wrap: inherit !important; word-break: inherit !important;">for</span> (<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(178, 148, 187); word-wrap: inherit !important; word-break: inherit !important;">auto</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(178, 148, 187); word-wrap: inherit !important; word-break: inherit !important;">const</span> &rate : rates)<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">8</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">std</span>::<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">cout</span> << rate << <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">std</span>::<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">endl</span>;<br></code></pre> <ul style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; padding-left: 32px; list-style-type: disc;"> <li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">在 C++17 中,經過使用結構化綁定和解構聲明:</span></li> </ul> <pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="cpp language-cpp hljs" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; background: rgb(29, 31, 33); color: rgb(197, 200, 198); padding: 0.5em; display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">1</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(178, 148, 187); word-wrap: inherit !important; word-break: inherit !important;">for</span> (<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(178, 148, 187); word-wrap: inherit !important; word-break: inherit !important;">auto</span> &&[rate, flag] : getRates2())<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">2</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">std</span>::<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">cout</span> << rate << <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">std</span>::<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); word-wrap: inherit !important; word-break: inherit !important;">endl</span>;<br></code></pre> <h3 id="hhowitworks" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid rgb(0, 172, 193);"><span style="font-size: inherit; line-height: inherit; margin: 0px; display: inline-block; font-weight: normal; background: rgb(0, 172, 193); color: rgb(255, 255, 255); padding: 3px 10px 0px; border-top-right-radius: 3px; border-top-left-radius: 4px; margin-right: 2px;">How it works…</span></h3> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">以前在 <em style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; font-style: italic;">How to do it…</em> 中顯示的基於範圍的 for 循環的表達式是基礎的語法糖,編譯器會將其轉換爲其它的東西。在 C++17 以前,編譯器生成的代碼以下所示:</p> <pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="cpp language-cpp hljs" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; background: rgb(29, 31, 33); color: rgb(197, 200, 198); padding: 0.5em; display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">1</span>{<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">2</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(178, 148, 187); word-wrap: inherit !important; word-break: inherit !important;">auto</span> &&__range = range_expression;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">3</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(178, 148, 187); word-wrap: inherit !important; word-break: inherit !important;">for</span> (<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(178, 148, 187); word-wrap: inherit !important; word-break: inherit !important;">auto</span> __begin = begin_expr, __end = end_expr;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">4</span> __begin != __end; ++__begin)<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">5</span> {<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">6</span> range_declaration = *__begin;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">7</span> loop_statement<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">8</span> }<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">9</span>}<br></code></pre> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">代碼中的 begin_expr 和 end_expr 依賴於這個範圍的類型:</p> <ul style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; padding-left: 32px; list-style-type: disc;"> <li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">對於 C 風格的數組,__range 和 __bound 是數組中元素的個數。</span></li> <li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">對於有 begin() 和 end() 成員的類類型(不管它們的類型和可訪問性):__range.begin() 和 __range.end()。</span></li> <li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">對於其它類型是 begin(__range) 和 end(__range),經過參數依賴查找進行肯定。</span></li> </ul> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">須要注意的是,若是一個類包含名爲 begin 或 end 的任何成員(函數,數據成員或枚舉器),不管它們的類型和可訪問性,它們都將被選爲 begin_expr 和 end_expr。所以,這樣的類類型不能使用基於範圍 for 循環。</p> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">在 C++17 中,編譯器生成的代碼略微不一樣:</p> <pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="cpp language-cpp hljs" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; background: rgb(29, 31, 33); color: rgb(197, 200, 198); padding: 0.5em; display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 1</span>{<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 2</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(178, 148, 187); word-wrap: inherit !important; word-break: inherit !important;">auto</span> &&__range = range_expression;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 3</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(178, 148, 187); word-wrap: inherit !important; word-break: inherit !important;">auto</span> __begin = begin_expr;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 4</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(178, 148, 187); word-wrap: inherit !important; word-break: inherit !important;">auto</span> __end = end_expr;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 5</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(178, 148, 187); word-wrap: inherit !important; word-break: inherit !important;">for</span> (; __begin != __end; ++__begin)<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 6</span> {<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 7</span> range_declaration = *__begin;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 8</span> loop_statement<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 9</span> }<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(222, 147, 95); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">10</span>}<br></code></pre> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">新標準移除了 begin 表達式和 end 表達式必須擁有同一類型的約束。end 表達式再也不須要是一個實際的迭代器,可是它必須可以與迭代器進行不等式比較。</p> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">這樣作的好處是範圍能夠由謂詞分隔。</p> <h3 id="hseealso" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid rgb(0, 172, 193);"><span style="font-size: inherit; line-height: inherit; margin: 0px; display: inline-block; font-weight: normal; background: rgb(0, 172, 193); color: rgb(255, 255, 255); padding: 3px 10px 0px; border-top-right-radius: 3px; border-top-left-radius: 4px; margin-right: 2px;">See also</span></h3> <ul style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; padding-left: 32px; list-style-type: disc;"> <li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">爲自定義類型啓用基於範圍的 for 循環</span></li> </ul> <p style="font-size: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; color: #FFFFFF; background-color: #13acc1; text-align: center;"><strong style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold;">- - - End - - -</strong></p> <hr style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; height: 1px; margin: 1.5rem 0px; border-right: none; border-bottom: none; border-left: none; border-image: initial; border-top: 1px dashed rgb(165, 165, 165);"> <figure style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><img src="https://lzl678-1253937507.cos.ap-chengdu.myqcloud.com/common/wechat-qcode.jpg" alt="歡迎掃碼訂閱個人微信公衆號,閱讀其它相關文章。" title="歡迎掃碼訂閱個人微信公衆號,閱讀其它相關文章。" style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; display: block; margin: 0px auto; max-width: 100%;"><figcaption style="line-height: inherit; margin: 0px; padding: 0px; margin-top: 10px; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;">歡迎掃碼訂閱個人微信公衆號,閱讀其它相關文章。</figcaption></figure> <blockquote style="line-height: inherit; display: block; padding: 15px 15px 15px 1rem; font-size: 0.9em; margin: 1em 0px; color: rgb(129, 145, 152); border-left: 6px solid rgb(220, 230, 240); background: rgb(242, 247, 251); overflow: auto; overflow-wrap: normal; word-break: normal;"> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 0px;"><strong style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold;">本文做者:</strong> Lzl678<br><strong style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold;">本文連接:</strong> <a href="https://www.cnblogs.com/Lzl678/p/10940182.html" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; color: rgb(30, 107, 184); overflow-wrap: break-word;">https://www.cnblogs.com/Lzl678/p/10940182.html</a><br><strong style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold;">版權聲明:</strong>本博客全部文章除特別聲明外,均採用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; color: rgb(30, 107, 184); overflow-wrap: break-word;">CC BY-NC-SA 4.0</a> 許可協議。轉載請註明出處!</p> </blockquote></div>html