<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