欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

C++中怎么使用模板提高代碼的抽象水平

本篇內(nèi)容主要講解“C++中怎么使用模板提高代碼的抽象水平”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“C++中怎么使用模板提高代碼的抽象水平”吧!

我們提供的服務(wù)有:成都網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、馬邊彝族ssl等。為數(shù)千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學管理、有技術(shù)的馬邊彝族網(wǎng)站制作公司

T.1:使用模板提高代碼的抽象水平

Reason(原因)

Generality. Reuse. Efficiency. Encourages consistent definition of user types.

普遍性。重用。效率。鼓勵用戶類型的一致性。

Example, bad(反面示例)

Conceptually, the following requirements are wrong because what we want of T is more than just the very low-level concepts of "can be incremented" or "can be added":

概念上,我們希望T不僅限于可以進行增量操作或者可以作為被加數(shù)這樣非常低水平的概念,因此下面的需求是錯誤的。

template<typename T>
   // requires Incrementable<T>
T sum1(vector<T>& v, T s)
{
   for (auto x : v) s += x;
   return s;
}

template<typename T>
   // requires Simple_number<T>
T sum2(vector<T>& v, T s)
{
   for (auto x : v) s = s + x;
   return s;
}

Assuming that Incrementable does not  support+ and Simple_number does not support +=, we have overconstrained implementers of sum1 and sum2. And, in this case, missed an opportunity for a generalization.

假設(shè)Incrementable不支持+而且Simple_number不支持+=,我們過分約束了sum1和sum2的實現(xiàn)者。而且,在這種情況下,失去了泛化的機會。

Example(示例)

template<typename T>
   // requires Arithmetic<T>
T sum(vector<T>& v, T s)
{
   for (auto x : v) s += x;
   return s;
}

Assuming that Arithmetic requires both + and +=, we have constrained the user of sum to provide a complete arithmetic type. That is not a minimal requirement, but it gives the implementer of algorithms much needed freedom and ensures that any Arithmetic type can be used for a wide variety of algorithms.

假設(shè)算術(shù)運算既需要+也需要+=,我們已經(jīng)要求sum的用戶提供完全的算術(shù)類型。這不是最小化的需求,但是它為算法的實現(xiàn)者提供了所需的更多自由,而且保證算術(shù)類型可以用于多種多樣的算法。

For additional generality and reusability, we could also use a more general Container or Range concept instead of committing to only one container, vector.

為了額外的泛用性和重用性,我們也可以使用更通用的容器或范圍概念代替特定的容器vector。

Note(注意)

If we define a template to require exactly the operations required for a single implementation of a single algorithm (e.g., requiring just += rather than also = and +) and only those, we have overconstrained maintainers. We aim to minimize requirements on template arguments, but the absolutely minimal requirements of an implementation is rarely a meaningful concept.

如果我們定義了一個要求用于特定算法的特定實現(xiàn)的操作的模板(例如只要求+=而不同時要求=和+)而且只要求這些,我們就過分約束維護者了。我們的目的在于最小化模板參數(shù)的需求,但是某一實現(xiàn)的絕對最小需求幾乎不會成為有意義的概念。

Note(注意)

Templates can be used to express essentially everything (they are Turing complete), but the aim of generic programming (as expressed using templates) is to efficiently generalize operations/algorithms over a set of types with similar semantic properties.

模板可以用于從本質(zhì)上表達任何東西(它們具備圖靈完備性),但是泛型編程的目的(像使用模板表達的那樣)是高效概括可以適用于具有相似語義屬性一套類型的操作/算法。

Note(注意)

The requires in the comments are uses of concepts. "Concepts" are defined in an ISO Technical Specification: concepts. Concepts are supported in GCC 6.1 and later. Consequently, we comment out uses of concepts in examples; that is, we use them as formalized comments only. If you use GCC 6.1 or later, you can uncomment them.

注釋中的需求是concept的用法。“Concepts”被定義在ISO技術(shù)規(guī)格中。GCC6.1之后的版本都支持Concepts。因此,在例子中我們注釋掉相關(guān)代碼;也就是說,我們只將它們用作標準注釋。如果你使用GCC6.1之后的版本,你可以去掉注釋符號。

Enforcement(實施建議)

  • Flag algorithms with "overly simple" requirements, such as direct use of specific operators without a concept.

  • 標記需求過于簡單的算法,例如不用concept而直接使用特定操作符。

  • Do not flag the definition of the "overly simple" concepts themselves; they may simply be building blocks for more useful concepts.

  • 不要標記定義過于簡單的concept本身;它們沒準只是作為某個更有用的concept的一部分存在的。

到此,相信大家對“C++中怎么使用模板提高代碼的抽象水平”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學習!

新聞名稱:C++中怎么使用模板提高代碼的抽象水平
文章轉(zhuǎn)載:http://aaarwkj.com/article28/iioicp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、自適應(yīng)網(wǎng)站定制開發(fā)、標簽優(yōu)化、網(wǎng)站內(nèi)鏈網(wǎng)站設(shè)計公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管
av永久天堂一区二区三区| 久久精品久久精品欧美大片| 亚洲成人不卡一区二区三区| 四虎永久精品在线视频| 97视频在线观看观看| 国产一区二区黄色网页| 99精品国产麻豆一区二区三区| 亚洲香蕉在线视频免费| 精品人妻少妇一区二区三| 国内在线免费观看av| 免费高清日本一区二区三区视频| 四虎精品国产一区二区三区| 欧美欧美欧美欧美一区| 最新日韩中文字幕在线播放| 日韩一区二区三区视频在线看| 欧美日韩亚洲中文字幕| 极品人妻少妇精品一区二区| 亚洲成人日韩成人av| 91亚色在线免费观看| 最新在线中文字幕av不卡| 欧美亚洲av一区二区三区| 日韩精品熟妻人女亚洲一区| 国产黄色片网站在线观看| 日韩精品一区二区三区电影在线播放 | 欧美精品激情在线不卡| 亚洲婷婷综合久久一区二区| 欧美丰满熟妇视频在线| 国产国产精品人在线观看 | 亚洲欧美成人免费视频| 亚洲一区二区三区有码| 亚洲一区二区视频在线观看免费| 少妇人妻系列中文在线| 久久久亚洲精品中文字幕蜜桃| 国产农村妇女一区二区三区 | 欧美中日韩一区二区三区| 午夜福利片免费在线观看| 亚洲最色一区二区三区| 国产美女口爆吞精久久| 中文字幕乱码熟女人妻视频 | 中文一级伦理一区二区| 亚洲综合国产中文字幕|