<!-- markdown css tag --><div class="pinggu_markdown">
<div class="pinggu_markdown__html"><p>需要<em>pointer</em>和<em>array</em>的前置知識,因為你會發(fā)現(xiàn)字符串和數(shù)組關(guān)系是很緊密的。<br>
首先,字符串是什么?本質(zhì)上來說字符串就是一組<em>characters</em>字符,字符是指字母,數(shù)字和符號這些東西,本質(zhì)上就是文本,對我們來說當(dāng)然會需要用某種方式把文本的形狀和格式在電腦上表示出來。what you should do , i don’t know how it works,</p>
<p>how characters work .</p>
<p>it is not about encoding</p>
<p>單一字符、整個段落、一個單詞、一堆單詞…所有這些被稱為字符串,<em>string of text</em>(文本字符串)</p>
<p>C++中有一種數(shù)字類型叫<em>char</em>,它是<em>character</em>的縮寫,占用 1 個字節(jié)內(nèi)存。它很有用,因為能把指針轉(zhuǎn)換為 char 類型指針,讓你根據(jù)字節(jié)進(jìn)行指針運算。它對分配內(nèi)存緩沖區(qū)也很有用,如果你想分配 1k 的內(nèi)存,你分配 1024 個 char 就行了。<br>
它對字符串和文本也很有用,因為 C++默認(rèn)處理的字符方式就是<em>ASCII</em>(aski)字符.<br>
字符也可以是大于一個字節(jié)的(比如中文、日文),可以有 2-4 個字節(jié)的字符。</p>
<p>如果我們只用 1 字節(jié)來表示字符,1 字節(jié)=8bit,這意味著我們有<span class="katex--inline"><span class="katex"><span class="katex-mathml"><math><semantics><mrow><msup><mn>2</mn><mn>8</mn></msup><mo>=</mo><mn>256</mn></mrow><annotation encoding="application/x-tex">2^8=256</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height: 0.814108em; vertical-align: 0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height: 0.814108em;"><span class="" style="top: -3.063em; margin-right: 0.05em;"><span class="pstrut" style="height: 2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">8</span></span></span></span></span></span></span></span><span class="mspace" style="margin-right: 0.277778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right: 0.277778em;"></span></span><span class="base"><span class="strut" style="height: 0.64444em; vertical-align: 0em;"></span><span class="mord">2</span><span class="mord">5</span><span class="mord">6</span></span></span></span></span> 種可能,顯然數(shù)量不夠,沒法適配所有語言。<br>
所以我們有了<em>utf-16</em>,也就是 16 位的字符編碼,這意味著我們有<span class="katex--inline"><span class="katex"><span class="katex-mathml"><math><semantics><mrow><msup><mn>2</mn><mn>16</mn></msup><mo>=</mo><mn>65536</mn></mrow><annotation encoding="application/x-tex">2^{16}=65536</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height: 0.814108em; vertical-align: 0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height: 0.814108em;"><span class="" style="top: -3.063em; margin-right: 0.05em;"><span class="pstrut" style="height: 2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span><span class="mord mtight">6</span></span></span></span></span></span></span></span></span><span class="mspace" style="margin-right: 0.277778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right: 0.277778em;"></span></span><span class="base"><span class="strut" style="height: 0.64444em; vertical-align: 0em;"></span><span class="mord">6</span><span class="mord">5</span><span class="mord">5</span><span class="mord">3</span><span class="mord">6</span></span></span></span></span> 種可能.</p>
<p>但是在 C++基礎(chǔ)語言中不適用任何庫只是原始數(shù)據(jù)類型的話,<em>char</em>就是 1 字節(jié)。fonts is huge complicated problem.</p>
<h2 id="const-char">1. const* char</h2>
<p>經(jīng)驗法則:如果你不用<em>new</em>關(guān)鍵字,那就不要用<em>delete</em></p>
<pre class=" language-cpp"><code class="prism language-cpp"> <span class="token keyword">const</span> <span class="token keyword">char</span><span class="token operator">*</span> name <span class="token operator">=</span> <span class="token string">"Cherno"</span><span class="token punctuation">;</span> <span class="token comment">// C語言風(fēng)格定義字符串的方式</span>
name<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'a'</span><span class="token punctuation">;</span> <span class="token comment">// 無法實現(xiàn),如果你知道不會去修改字符串就加上const,否則就去掉</span>
</code></pre>
<p>C++11 開始,將字符串常量直接賦值給非<code>const</code>的<code>char*</code>指針被視為不安全的行為。<br>
故用</p>
<pre class=" language-cpp"><code class="prism language-cpp"><span class="token keyword">char</span> name<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">"Cherno"</span><span class="token punctuation">;</span>
</code></pre>
<p><img src="./storage%20bag/Pasted%20image%2020230705122342.png" alt=""></p>
<p>圖中給的<code>00</code>字符被稱為<em>the null termination character</em>(空終止符),這樣我們就知道字符串在哪里結(jié)束,也便于知道字符串大小 ^26e746</p>
<pre class=" language-cpp"><code class="prism language-cpp"><span class="token keyword">char</span> name2<span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token string">'C'</span><span class="token punctuation">,</span><span class="token string">'h'</span><span class="token punctuation">,</span><span class="token string">'e'</span><span class="token punctuation">,</span><span class="token string">'r'</span><span class="token punctuation">,</span><span class="token string">'n'</span><span class="token punctuation">,</span><span class="token string">'o'</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
std<span class="token operator">::</span>cout <span class="token operator"><<</span> name2 <span class="token operator"><<</span> std<span class="token operator">::</span>endl<span class="token punctuation">;</span> <span class="token comment">// Cherno燙燙燙燙燙......</span>
</code></pre>
<p><img src="./storage%20bag/Pasted%20image%2020230705123511.png" alt=""></p>
<p>分配了***array guard***,但是沒有空終止符,所以 cout 不知道打印到哪里結(jié)束</p>
<pre class=" language-cpp"><code class="prism language-cpp"><span class="token keyword">char</span> name2<span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token string">'C'</span><span class="token punctuation">,</span><span class="token string">'h'</span><span class="token punctuation">,</span><span class="token string">'e'</span><span class="token punctuation">,</span><span class="token string">'r'</span><span class="token punctuation">,</span><span class="token string">'n'</span><span class="token punctuation">,</span><span class="token string">'o'</span><span class="token punctuation">,</span><span class="token string">'\0'</span><span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token comment">// 直接寫成0也行</span>
</code></pre>
<p><img src="./storage%20bag/Pasted%20image%2020230705123808.png" alt=""><br>
正常輸出</p>
<h2 id="stdstring">2. std::string</h2>
<p>std::string 有個接受參數(shù)為 char 指針或者 const char 指針的構(gòu)造函數(shù)<br>
<img src="./storage%20bag/Pasted%20image%2020230705124249.png" alt=""><br>
還有很多內(nèi)置方法</p>
<pre class=" language-cpp"><code class="prism language-cpp">std<span class="token operator">::</span>string name <span class="token operator">=</span> <span class="token string">"Cherno"</span><span class="token punctuation">;</span>
std<span class="token operator">::</span>cout <span class="token operator"><<</span> name <span class="token operator"><<</span> std<span class="token operator">::</span>endl<span class="token punctuation">;</span> <span class="token comment">// Cherno</span>
std<span class="token operator">::</span>cout <span class="token operator"><<</span> name<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator"><<</span> std<span class="token operator">::</span>endl<span class="token punctuation">;</span> <span class="token comment">// 6</span>
</code></pre>
<h3 id="字符串拼接">字符串拼接</h3>
<pre class=" language-cpp"><code class="prism language-cpp">std<span class="token operator">::</span>string name <span class="token operator">=</span> <span class="token string">"Cherno"</span> <span class="token operator">+</span> <span class="token string">"hello"</span><span class="token punctuation">;</span> <span class="token comment">//'+': cannot add two pointers</span>
</code></pre>
<p>上面提到了它們是 const char 數(shù)組,不是真正的 string</p>
<pre class=" language-cpp"><code class="prism language-cpp">std<span class="token operator">::</span>string name <span class="token operator">=</span> <span class="token string">"Cherno"</span><span class="token punctuation">;</span>
name <span class="token operator">+</span><span class="token operator">=</span> <span class="token string">"hello"</span><span class="token punctuation">;</span> <span class="token comment">// Chernohello</span>
</code></pre>
<p>或者</p>
<pre class=" language-cpp"><code class="prism language-cpp">std<span class="token operator">::</span>string name <span class="token operator">=</span> std<span class="token operator">::</span><span class="token function">string</span><span class="token punctuation">(</span><span class="token string">"Cherno"</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">"hello!"</span><span class="token punctuation">;</span>
</code></pre>
<h2 id="字符串傳給其他函數(shù)">3. 字符串傳給其他函數(shù)</h2>
<pre class=" language-cpp"><code class="prism language-cpp"><span class="token keyword">void</span> <span class="token function">PrintString</span><span class="token punctuation">(</span><span class="token keyword">const</span> std<span class="token operator">::</span>string<span class="token operator">&</span> string<span class="token punctuation">)</span> <span class="token comment">//引用,而不會拷貝,并保證不修改</span>
</code></pre>
</div>
</div>