<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          C++核心準(zhǔn)則C.100:定義容器時(shí)遵從STL標(biāo)準(zhǔn)?

          77d690cd5424c63ca6e904ef0de02e67.webp

          29ac92de4164262b8f252f1879e9401b.webp4113d0f6df9ae55352d8a5b272d0c3ab.webp2c2c8133a21b591c2e865e404064c380.webp

          C.100: Follow the STL when defining a container

          C.100:定義容器時(shí)遵從STL標(biāo)準(zhǔn)?

          40173e05b65e0b93172af30f57437ec4.webp4113d0f6df9ae55352d8a5b272d0c3ab.webp832854eab7f39597ef82b198b7e91039.webp



          9d54973615455d8312106859002e0d74.webp

          Reason(原因)



          The STL containers are familiar to most C++ programmers and a fundamentally sound design.

          STL容器被大多數(shù)程序員所熟知,是非常完美的設(shè)計(jì)。



          9d54973615455d8312106859002e0d74.webp

          Note(注意)?


          There are of course other fundamentally sound design styles and sometimes reasons to depart from the style of the standard library, but in the absence of a solid reason to differ, it is simpler and easier for both implementers and users to follow the standard.

          當(dāng)然存在其他的完美設(shè)計(jì),有時(shí)也存在違背標(biāo)準(zhǔn)庫(kù)風(fēng)格的進(jìn)行設(shè)計(jì)的理由,但如果沒(méi)有足夠充分的理由,遵照標(biāo)準(zhǔn)庫(kù)風(fēng)格對(duì)于實(shí)現(xiàn)者和使用者雙方都簡(jiǎn)單和容易。

          In particular, std::vector and std::map provide useful relatively simple models.

          特別是std::vector和std::map,提供了有用且相當(dāng)簡(jiǎn)單的模型。



          9d54973615455d8312106859002e0d74.webp

          Example(示例)?


          //?simplified?(e.g.,?no?allocators):

          template
          class?Sorted_vector?{
          ????using?value_type?=?T;
          ????//?...?iterator?types?...

          ????Sorted_vector()?=?default;
          ????Sorted_vector(initializer_list);????//?initializer-list?constructor:?sort?and?store
          ????Sorted_vector(const?Sorted_vector&)?=?default;
          ????Sorted_vector(Sorted_vector&&)?=?default;
          ????Sorted_vector&?operator=(const?Sorted_vector&)?=?default;???//?copy?assignment
          ????Sorted_vector&?operator=(Sorted_vector&&)?=?default;????????//?move?assignment
          ????~Sorted_vector()?=?default;

          ????Sorted_vector(const?std::vector&?v);???//?store?and?sort
          ????Sorted_vector(std::vector&&?v);????????//?sort?and?"steal?representation"

          ????const?T&?operator[](int?i)?const?{?return?rep[i];?}
          ????//?no?non-const?direct?access?to?preserve?order

          ????void?push_back(const?T&);???//?insert?in?the?right?place?(not?necessarily?at?back)
          ????void?push_back(T&&);????????//?insert?in?the?right?place?(not?necessarily?at?back)

          ????//?...?cbegin(),?cend()?...
          private:
          ????std::vector?rep;??//?use?a?std::vector?to?hold?elements
          };

          template?bool?operator==(const?Sorted_vector&,?const?Sorted_vector&);
          template?bool?operator!=(const?Sorted_vector&,?const?Sorted_vector&);
          //?...

          Here, the STL style is followed, but incompletely. That's not uncommon. Provide only as much functionality as makes sense for a specific container. The key is to define the conventional constructors, assignments, destructors, and iterators (as meaningful for the specific container) with their conventional semantics. From that base, the container can be expanded as needed. Here, special constructors from std::vector were added.

          這里遵守了標(biāo)準(zhǔn)庫(kù)風(fēng)格,但是不完全。這沒(méi)有什么特別。應(yīng)該提供構(gòu)成特定容器的需要的功能。關(guān)鍵是定義帶有常規(guī)語(yǔ)義的符合常規(guī)的構(gòu)造函數(shù),復(fù)制運(yùn)算符,析構(gòu)函數(shù)和迭代器(對(duì)于特殊容器有意義)。以此為基礎(chǔ),容器可以按照需要進(jìn)行擴(kuò)展。這里增加了來(lái)自std::vector的特殊構(gòu)造函數(shù)。




          9d54973615455d8312106859002e0d74.webp

          Enforcement(實(shí)施建議)


          ???




          9d54973615455d8312106859002e0d74.webp

          原文鏈接


          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c100-follow-the-stl-when-defining-a-container




          覺(jué)得本文有幫助?請(qǐng)分享給更多人。

          關(guān)注【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!

          面向?qū)ο箝_(kāi)發(fā),面向?qū)ο笏伎迹?/span>

          瀏覽 74
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  人人妻人人摸 | 殴美肏屄视频 | 三级在线播放视频 | 欧美日韩午夜激情 | 操逼视频网站大全 |