<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="https://shaozk.xyz/feed_style.xsl" type="text/xsl"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <tabi:metadata xmlns:tabi="https://github.com/welpo/tabi">
        <tabi:base_url>https:&#x2F;&#x2F;shaozk.xyz</tabi:base_url>
        <tabi:separator>
            •
        </tabi:separator>
        <tabi:about_feeds>This is a web feed, also known as an Atom feed. Subscribe by copying the URL from the address bar into your newsreader. Visit About Feeds to learn more and get started. It&#x27;s free.</tabi:about_feeds>
        <tabi:visit_the_site>Visit website</tabi:visit_the_site>
        <tabi:recent_posts>Recent posts</tabi:recent_posts>
        <tabi:last_updated_on>Updated on $DATE</tabi:last_updated_on>
        <tabi:default_theme></tabi:default_theme>
        <tabi:post_listing_date>date</tabi:post_listing_date>
        <tabi:current_section>c++17</tabi:current_section>
    </tabi:metadata><title>shaozk - c++17</title>
    <link href="https://shaozk.xyz/tags/c-17/atom.xml" rel="self" type="application/atom+xml"/>
    <link href="https://shaozk.xyz/tags/c-17/" rel="alternate" type="text/html"/>
    <generator uri="https://www.getzola.org/">Zola</generator><updated>2026-07-05T00:00:00+00:00</updated><id>https://shaozk.xyz/tags/c-17/atom.xml</id><entry xml:lang="en">
        <title>使用 std::string_view 替代 const std::string&amp;</title>
        <published>2026-07-05T00:00:00+00:00</published>
        <updated>2026-07-05T00:00:00+00:00</updated>
        <author>
            <name>shaozk</name>
        </author>
        <link rel="alternate" href="https://shaozk.xyz/blog/003-use-string-view-instead-of-string/" type="text/html"/>
        <id>https://shaozk.xyz/blog/003-use-string-view-instead-of-string/</id>
        
            <content type="html">&lt;p&gt;C++17 中增加了 std::string_view 新特性，表示非占有数据的字符序列。非占有数据如何理解？&lt;&#x2F;p&gt;
&lt;p&gt;主要包含三个关键点：&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;可以表征字符序列&lt;&#x2F;p&gt;
&lt;p&gt;C-Style string 和 C++-Style std::string ，都可以用 std:string_view 表示，默认会进行隐式转换。&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;不占有数据存储空间&lt;&#x2F;p&gt;
&lt;p&gt;只存储指向原始数据的指针和长度，不会增加数据拷贝的额外负担。&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;无法对数据进行修改&lt;&#x2F;p&gt;
&lt;p&gt;你绝对无法修改其数据内容，这也是名字 &lt;code&gt;view&lt;&#x2F;code&gt; 的由来之一。如果你需要对字符串进行修改，那就不该使用它。&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;有了这些特性，仅仅使用 std::string_view 作为函数形参，就可以实现类似常量引用的效果，不再需要额外的 const 和 reference 修饰符。&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;&#x2F;&#x2F; old&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; print_str&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;const&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-scope-resolution z-parameter z-cpp&quot;&gt; std&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;::&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt;string&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier z-reference z-cpp&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt; str&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-scope-resolution z-cpp&quot;&gt;    std&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;::&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;cout &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; str &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-scope-resolution z-cpp&quot;&gt; std&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;::&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;endl&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;&#x2F;&#x2F; now&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; print_sv&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-scope-resolution z-parameter z-cpp&quot;&gt;std&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;::&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt;string_view&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt; sv&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-scope-resolution z-cpp&quot;&gt;    std&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;::&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;cout &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; str &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-scope-resolution z-cpp&quot;&gt; std&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;::&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;endl&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;然和在《Effective C++》中学到的经验告诉我们尽量 pass-by-reference 而不是 pass-by-value ，当然前提是传值的开销远大于参数参数时。那么 std::string_view 实例对象占用了多大空间呢？&lt;&#x2F;p&gt;
&lt;p&gt;在两大开源编译器 g++ 和 clang++ 的实现中，std:string_view 的数据成员仅有两个，一个是指向字符序列的常量指针，另一个是序列大小。&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;&#x2F;&#x2F; gnu g++&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;private&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;    size_t&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;	    _M_len&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    const&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; _CharT&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;*&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; _M_str&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;&#x2F;&#x2F; llvm clang++&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;private&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    const_pointer data_&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;  &#x2F;&#x2F; exposition only&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    size_type     size_&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;  &#x2F;&#x2F; exposition only&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;在 64 位机器上，仅占用 128 bit(16 bytes) 的空间。pass-by-value 和 pass-by-reference 的开销相差无几。&lt;&#x2F;p&gt;
&lt;p&gt;另外，std::string_view 支持哈希，可以直接在 std::unordered_map 及其他字典容器中作为 key 使用。&lt;&#x2F;p&gt;
&lt;h1 id=&quot;xue-xi&quot;&gt;学习&lt;&#x2F;h1&gt;
&lt;p&gt;https:&#x2F;&#x2F;en.cppreference.com&#x2F;cpp&#x2F;string&#x2F;basic_string_view
https:&#x2F;&#x2F;www.learncpp.com&#x2F;cpp-tutorial&#x2F;introduction-to-stdstring_view&#x2F;
https:&#x2F;&#x2F;learnmoderncpp.com&#x2F;2024&#x2F;10&#x2F;29&#x2F;when-to-use-stdstring_view&#x2F;
https:&#x2F;&#x2F;www.open-std.org&#x2F;jtc1&#x2F;sc22&#x2F;wg21&#x2F;docs&#x2F;papers&#x2F;2014&#x2F;n3921.html&lt;&#x2F;p&gt;
</content>
        </entry>
</feed>
