Skip to content

Commit

Permalink
deploy: 1496935
Browse files Browse the repository at this point in the history
  • Loading branch information
archibate committed Sep 24, 2024
1 parent d61b6f5 commit bb2e961
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
17 changes: 15 additions & 2 deletions cpp_tricks/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1243,9 +1243,22 @@ <h2 id="cout-endl">cout 不需要 endl</h2>
</code></pre>
<p>endl 是一个典型的以讹传讹错误写法,只有当你的输出是指向另一个进程的管道时,其附带的刷新功能才有作用。</p>
<ul>
<li>当输出是管道时<code>cout</code> 需要 <code>endl</code> 才能刷新。</li>
<li>当输出是普通控制台时,<code>cout</code> 需要 <code>endl</code> 才能刷新</li>
<li>当输出是管道或文件时<code>cout</code> 需要 <code>endl</code> 才能刷新。</li>
<li>当输出是普通控制台时,<code>cout</code> 只需 <code>'\n'</code> 就能刷新了,根本用不着 <code>endl</code></li>
</ul>
<p>而且,管道或文件实际上也不存在频繁刷新的需求,反正 <code>ifstream</code> 析构时总是会自动刷新写入磁盘。</p>
<p>因此,<code>endl</code> 操纵符大多时候都是冗余的:控制台输出的 <code>cout</code> 只需要字符或字符串中含有 <code>'\n'</code> 就刷新了,即使是文件读写也很少会使用 <code>endl</code></p>
<p>如果确实需要强制刷新,也可以用 <code>flush</code> 这种更加可读的写法:</p>
<pre><code class="language-cpp">int num;
cout &lt;&lt; &quot;please input the number: &quot; &lt;&lt; flush;
cin &gt;&gt; num;

ofstream fout(&quot;log.txt&quot;);
fout &lt;&lt; &quot;immediate write 1\n&quot; &lt;&lt; flush;
sleep(1);
fout &lt;&lt; &quot;immediate write 2\n&quot; &lt;&lt; flush;
fout.close(); // 关闭文件时总是自动 flush,不会有残留未写入的字符
</code></pre>
<h2 id="cout">多线程中 cout 出现乱序?</h2>
<p>同学:小彭老师,我在多线程环境中使用:</p>
<pre><code class="language-cpp">cout &lt;&lt; &quot;the answer is &quot; &lt;&lt; 42 &lt;&lt; '\n';
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ <h2 id="_1">前言</h2>
<blockquote>
<p><img src="./img/bulb.png" height="30px" width="auto" style="margin: 0; border: none"/> 本书还在持续更新中……要追番的话,可以在 <a href="https://github.com/parallel101/cppguidebook">GitHub</a> 点一下右上角的 “Watch” 按钮,每当小彭老师提交新 commit,GitHub 会向你发送一封电子邮件,提醒你小彭老师更新了。</p>
</blockquote>
<p>更新时间:2024年09月22日 21:08:12 (UTC+08:00)</p>
<p>更新时间:2024年09月24日 11:54:32 (UTC+08:00)</p>
<p><a href="https://parallel101.github.io/cppguidebook">在 GitHub Pages 浏览本书</a> | <a href="https://142857.red/book">在小彭老师自己维护的镜像上浏览本书</a></p>
<h2 id="_2">格式约定</h2>
<blockquote>
Expand Down
21 changes: 17 additions & 4 deletions print_page/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ <h2 id="index-_1">前言</h2>
<blockquote>
<p><img src="../img/bulb.png" height="30px" width="auto" style="margin: 0; border: none"/> 本书还在持续更新中……要追番的话,可以在 <a href="https://github.com/parallel101/cppguidebook">GitHub</a> 点一下右上角的 “Watch” 按钮,每当小彭老师提交新 commit,GitHub 会向你发送一封电子邮件,提醒你小彭老师更新了。</p>
</blockquote>
<p>更新时间:2024年09月22日 21:08:12 (UTC+08:00)</p>
<p>更新时间:2024年09月24日 11:54:32 (UTC+08:00)</p>
<p><a href="https://parallel101.github.io/cppguidebook">在 GitHub Pages 浏览本书</a> | <a href="https://142857.red/book">在小彭老师自己维护的镜像上浏览本书</a></p>
<h2 id="index-_2">格式约定</h2>
<blockquote>
Expand Down Expand Up @@ -1841,9 +1841,22 @@ <h2 id="cpp_tricks-cout-endl">cout 不需要 endl</h2>
</code></pre>
<p>endl 是一个典型的以讹传讹错误写法,只有当你的输出是指向另一个进程的管道时,其附带的刷新功能才有作用。</p>
<ul>
<li>当输出是管道时,<code>cout</code> 需要 <code>endl</code> 才能刷新。</li>
<li>当输出是普通控制台时,<code>cout</code> 需要 <code>endl</code> 才能刷新。</li>
</ul>
<li>当输出是管道或文件时,<code>cout</code> 需要 <code>endl</code> 才能刷新。</li>
<li>当输出是普通控制台时,<code>cout</code> 只需 <code>'\n'</code> 就能刷新了,根本用不着 <code>endl</code>。</li>
</ul>
<p>而且,管道或文件实际上也不存在频繁刷新的需求,反正 <code>ifstream</code> 析构时总是会自动刷新写入磁盘。</p>
<p>因此,<code>endl</code> 操纵符大多时候都是冗余的:控制台输出的 <code>cout</code> 只需要字符或字符串中含有 <code>'\n'</code> 就刷新了,即使是文件读写也很少会使用 <code>endl</code>。</p>
<p>如果确实需要强制刷新,也可以用 <code>flush</code> 这种更加可读的写法:</p>
<pre><code class="language-cpp">int num;
cout &lt;&lt; &quot;please input the number: &quot; &lt;&lt; flush;
cin &gt;&gt; num;

ofstream fout(&quot;log.txt&quot;);
fout &lt;&lt; &quot;immediate write 1\n&quot; &lt;&lt; flush;
sleep(1);
fout &lt;&lt; &quot;immediate write 2\n&quot; &lt;&lt; flush;
fout.close(); // 关闭文件时总是自动 flush,不会有残留未写入的字符
</code></pre>
<h2 id="cpp_tricks-cout">多线程中 cout 出现乱序?</h2>
<p>同学:小彭老师,我在多线程环境中使用:</p>
<pre><code class="language-cpp">cout &lt;&lt; &quot;the answer is &quot; &lt;&lt; 42 &lt;&lt; '\n';
Expand Down
2 changes: 1 addition & 1 deletion search/search_index.json

Large diffs are not rendered by default.

Binary file modified sitemap.xml.gz
Binary file not shown.

0 comments on commit bb2e961

Please sign in to comment.