@@ -421,7 +421,7 @@ <h2 id="index-_1">前言</h2>
421
421
<blockquote>
422
422
<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>
423
423
</blockquote>
424
- <p>更新时间:2024年10月25日 12:47:54 (UTC+08:00)</p>
424
+ <p>更新时间:2024年10月25日 12:54:18 (UTC+08:00)</p>
425
425
<p><a href="https://parallel101.github.io/cppguidebook">在 GitHub Pages 浏览本书</a> | <a href="https://142857.red/book">在小彭老师自己维护的镜像上浏览本书</a></p>
426
426
<h2 id="index-_2">格式约定</h2>
427
427
<blockquote>
@@ -1130,7 +1130,7 @@ <h2 id="symbols-linkage">符号的链接类型 (linkage)</h2>
1130
1130
<li><a href="#cpp_tricks-adl">ADL 机制实现静态多态</a></li>
1131
1131
<li><a href="#cpp_tricks-shared_from_this">shared_from_this</a></li>
1132
1132
<li><a href="#cpp_tricks-requires">requires 语法检测是否存在指定成员函数</a></li>
1133
- <li><a href="#cpp_tricks-locale-utf8_1 ">设置 locale 为 .utf8 解决编码问题 </a></li>
1133
+ <li><a href="#cpp_tricks-locale-utf8-windows ">设置 locale 为 .utf8 解决 Windows 编码难问题 </a></li>
1134
1134
<li><a href="#cpp_tricks-this_1">成员函数针对 this 的移动重载</a></li>
1135
1135
<li><a href="#cpp_tricks-bit-field">位域(bit-field)</a></li>
1136
1136
<li><a href="#cpp_tricks-vector-unordered_map-lru-cache">vector + unordered_map = LRU cache</a></li>
@@ -1381,7 +1381,7 @@ <h2 id="cpp_tricks-_7">继承构造函数</h2>
1381
1381
1382
1382
Child child(23, "peng"); // 错误!Child 没有构造函数!
1383
1383
</code></pre>
1384
- <p>可以在子类里面写 <code>using 父类::父类</code>,就能自动继承父类所有的构造函数了。</p>
1384
+ <p>C++11 中可以在子类里面写 <code>using 父类::父类</code>,就能自动继承父类所有的构造函数了。</p>
1385
1385
<pre><code class="language-cpp">struct Parent {
1386
1386
Parent(int age, const char *name) { ... }
1387
1387
void parent_func() { ... }
@@ -2624,7 +2624,13 @@ <h2 id="cpp_tricks-adl">ADL 机制实现静态多态</h2>
2624
2624
<p>TODO</p>
2625
2625
<h2 id="cpp_tricks-shared_from_this">shared_from_this</h2>
2626
2626
<h2 id="cpp_tricks-requires">requires 语法检测是否存在指定成员函数</h2>
2627
- <h2 id="cpp_tricks-locale-utf8_1">设置 locale 为 .utf8 解决编码问题</h2>
2627
+ <h2 id="cpp_tricks-locale-utf8-windows">设置 locale 为 .utf8 解决 Windows 编码难问题</h2>
2628
+ <pre><code class="language-cpp">system("chcp 65001");
2629
+ setlocale("LC_ALL", ".utf-8");
2630
+ </code></pre>
2631
+ <blockquote>
2632
+ <p><img src="../img/bulb.png" height="30px" width="auto" style="margin: 0; border: none"/> 详见 <a href="#unicode">Unicode 专题章节</a>。</p>
2633
+ </blockquote>
2628
2634
<h2 id="cpp_tricks-this_1">成员函数针对 this 的移动重载</h2>
2629
2635
<h2 id="cpp_tricks-bit-field">位域(bit-field)</h2>
2630
2636
<p>在互联网编程和各种与硬盘、序列化打交道的场景中,常常需要按位拆分单个字节。</p>
@@ -2651,7 +2657,36 @@ <h2 id="cpp_tricks-lambda-unique_ptr-function">Lambda 捕获 unique_ptr 导致 f
2651
2657
<h2 id="cpp_tricks-_20">多线程通信应基于队列,而不是共享全局变量</h2>
2652
2658
<h2 id="cpp_tricks-raii-finally">RAII 的 finally 帮手类</h2>
2653
2659
<h2 id="cpp_tricks-swap-mutex">swap 缩小 mutex 区间代价</h2>
2654
- <h2 id="cpp_tricks-namespace">namespace 别名</h2></section><section class="print-page" id="lambda"><h1 id="lambda-_1">小彭老师带你学函数式编程</h1>
2660
+ <h2 id="cpp_tricks-namespace">namespace 别名</h2>
2661
+ <p>有些嵌套很深的名字空间每次都要复读非常啰嗦。</p>
2662
+ <pre><code class="language-cpp">#include <filesystem>
2663
+
2664
+ int main() {
2665
+ std::filesystem::path p = "/var/www/html";
2666
+ ...
2667
+ }
2668
+ </code></pre>
2669
+ <p>如果 <code>using namespace</code> 的话,又觉得污染全局名字空间了。</p>
2670
+ <pre><code class="language-cpp">#include <filesystem>
2671
+
2672
+ using namespace std::filesystem;
2673
+
2674
+ int main() {
2675
+ std::filesystem::path p = "/var/www/html";
2676
+ ...
2677
+ }
2678
+ </code></pre>
2679
+ <p>可以用 C++11 的 <code>namespace =</code> 语法,给名字空间取个别名。</p>
2680
+ <pre><code class="language-cpp">#include <filesystem>
2681
+
2682
+ namespace fs = std::filesystem;
2683
+
2684
+ int main() {
2685
+ fs::path p = "/var/www/html";
2686
+ ...
2687
+ }
2688
+ </code></pre>
2689
+ <p>这样以后就可以 <code>fs</code> 这个简称访问了。</p></section><section class="print-page" id="lambda"><h1 id="lambda-_1">小彭老师带你学函数式编程</h1>
2655
2690
<div class="toc">
2656
2691
<ul>
2657
2692
<li><a href="#lambda-_1">小彭老师带你学函数式编程</a><ul>
0 commit comments