<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)則?SL.con.1:標(biāo)準(zhǔn)庫array或vector好于C數(shù)組

          SL.con.1: Prefer using STL?array?or?vector?instead of a C array

          SL.con.1:標(biāo)準(zhǔn)庫array或vector好于C數(shù)組


          Reason(原因)

          C arrays are less safe, and have no advantages over?array?and?vector. For a fixed-length array, use?std::array, which does not degenerate to a pointer when passed to a function and does know its size. Also, like a built-in array, a stack-allocated?std::array?keeps its elements on the stack. For a variable-length array, use?std::vector, which additionally can change its size and handles memory allocation.

          C數(shù)組不夠安全,和array或者vector相比沒有任何優(yōu)勢(shì)。對(duì)于固定長度數(shù)組來講,使用std::array,當(dāng)被傳遞給某個(gè)函數(shù)時(shí),它不會(huì)退化成指針無法獲得長度。同時(shí)和內(nèi)置的數(shù)組一樣,堆棧上分配的std::array將元素保存在堆棧上。對(duì)于可變長度數(shù)組,使用std::vector,它可以進(jìn)一步提供變更長度和慣例內(nèi)存分配的功能。


          Example(示例)

          int v[SIZE];                        // BAD

          std::array w; // ok
          Example(示例)
          int* v = new int[initial_size];     // BAD, owning raw pointer
          delete[] v; // BAD, manual delete

          std::vector w(initial_size); // ok
          Note(注意)

          Use?gsl::span?for non-owning references into a container.

          對(duì)于不包含所有權(quán)的容器參照,使用gsl::span


          Note(注意)

          Comparing the performance of a fixed-sized array allocated on the stack against a?vector?with its elements on the free store is bogus. You could just as well compare a?std::array?on the stack against the result of a?malloc()?accessed through a pointer. For most code, even the difference between stack allocation and free-store allocation doesn't matter, but the convenience and safety of?vector?does. People working with code for which that difference matters are quite capable of choosing between?array?and?vector.

          在分配于堆棧上固定長度數(shù)組和將元素分配于自由存儲(chǔ)上的vector之間進(jìn)行性能比較是沒有意義的。比較指針訪問堆棧上分配的std::array和malloc的結(jié)果倒是有些意義。對(duì)于大多數(shù)代碼,堆棧分配和自由存儲(chǔ)分配的(性能,譯者注)區(qū)別沒什么影響,然而vector卻可以提供便利性和安全性。如果有些代碼確實(shí)對(duì)這種區(qū)別敏感,人們完全可以在array和vector之間進(jìn)行選擇。


          Enforcement(實(shí)施建議)

          • Flag declaration of a C array inside a function or class that also declares an STL container (to avoid excessive noisy warnings on legacy non-STL code). To fix: At least change the C array to a?std::array.

            標(biāo)記同時(shí)在函數(shù)或類內(nèi)部同時(shí)使用C數(shù)組和STL容器的情況(為了避免對(duì)既存的非STL代碼過度報(bào)警)。修改方法:至少將C風(fēng)格數(shù)組替換為std::array。


          原文鏈接

          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#slcon1-prefer-using-stl-array-or-vector-instead-of-a-c-array


          新書介紹

          《實(shí)戰(zhàn)Python設(shè)計(jì)模式》是作者最近出版的新書,拜托多多關(guān)注!

          本書利用Python 的標(biāo)準(zhǔn)GUI 工具包tkinter,通過可執(zhí)行的示例對(duì)23 個(gè)設(shè)計(jì)模式逐個(gè)進(jìn)行說明。這樣一方面可以使讀者了解真實(shí)的軟件開發(fā)工作中每個(gè)設(shè)計(jì)模式的運(yùn)用場景和想要解決的問題;另一方面通過對(duì)這些問題的解決過程進(jìn)行說明,讓讀者明白在編寫代碼時(shí)如何判斷使用設(shè)計(jì)模式的利弊,并合理運(yùn)用設(shè)計(jì)模式。

          對(duì)設(shè)計(jì)模式感興趣而且希望隨學(xué)隨用的讀者通過本書可以快速跨越從理解到運(yùn)用的門檻;希望學(xué)習(xí)Python GUI 編程的讀者可以將本書中的示例作為設(shè)計(jì)和開發(fā)的參考;使用Python 語言進(jìn)行圖像分析、數(shù)據(jù)處理工作的讀者可以直接以本書中的示例為基礎(chǔ),迅速構(gòu)建自己的系統(tǒng)架構(gòu)。




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

          關(guān)注微信公眾號(hào)【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!

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



          瀏覽 44
          點(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>
                  亚洲大鸡巴 | 一级a一级a爱片免费免会永久 | 操你逼| 91干在线视频 | 久色网站|