日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

c 是泛型程序设计语言,c ++中的“泛型编程”是什么意思?

發(fā)布時(shí)間:2023/12/19 57 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c 是泛型程序设计语言,c ++中的“泛型编程”是什么意思? 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

容器

在C ++中,容器是允許您存儲(chǔ)對(duì)象的類(lèi)。 例如,標(biāo)準(zhǔn)庫(kù)std::list l是可調(diào)整大小的數(shù)組,用于存儲(chǔ)某種類(lèi)型T的對(duì)象。為了正式地被認(rèn)為是容器類(lèi),它必須公開(kāi)某些功能以便于通用編程。 我可以引用C ++標(biāo)準(zhǔn)的確切要求,但在大多數(shù)情況下,重要的容器類(lèi)是標(biāo)準(zhǔn)庫(kù)中的容器:std::list、deque、list、map、set和2687499344584115115/2/multiset。

重要要求之一是它們必須允許迭代器訪問(wèn)。

迭代器

“迭代器”在這里可能意味著兩件事:它是設(shè)計(jì)模式的名稱,但是在C ++中,它也是該設(shè)計(jì)模式的特定表達(dá)式的名稱。 C ++迭代器是一種類(lèi)型的類(lèi)型,它允許使用類(lèi)似指針的語(yǔ)法遍歷一系列元素。

例如,如果您有一個(gè)數(shù)組std::list l,則可以使用普通指針作為迭代器:

int* first = a; // create an iterator that points to the beginning of the array

++first; // make the iterator point to the second element

int i = *first; // get the value of the element pointed to by the iterator

int* last = a+10; //create an "end" iterator, one which points one past the end of the array

如果我有一個(gè)鏈表,例如std::list l,我可以做很多事情,盡管現(xiàn)在我的迭代器不再只是指針,而是實(shí)現(xiàn)為專門(mén)用于std::list的類(lèi)類(lèi)型:

std::list::iterator first = l.begin(); // create an iterator that points to the beginning of the list

++first; // make the iterator point to the second element

int i = *first; // get the value of the element pointed to by the iterator

std::list::iterator last = l.end(); //create an "end" iterator, one which points one past the end of the list

或帶有向量IIterator:

std::vector::iterator first = v.begin(); // create an iterator that points to the beginning of the vector

++first; // make the iterator point to the second element

int i = *first; // get the value of the element pointed to by the iterator

std::list::iterator last = v.end(); //create an "end" iterator, one which points one past the end of the vector

關(guān)于迭代器的重要之處在于,它們?yōu)槲覀兲峁┝吮闅v元素序列的統(tǒng)一語(yǔ)法,無(wú)論序列如何存儲(chǔ)在內(nèi)存中(甚至即使存儲(chǔ)在內(nèi)存中也是如此。可以編寫(xiě)迭代器以迭代a的內(nèi)容) 磁盤(pán)上的數(shù)據(jù)庫(kù),或者我們也可以使用迭代器包裝器使流(例如IIterator)看起來(lái)也像一系列對(duì)象:

std::istream_iterator(std::cin) first;

++first; // make the iterator point to the second element

int i = *first; // get the value of the element pointed to by the iterator

std::list::iterator last; //create an "end" iterator, which marks the end of the stream

盡管因?yàn)樗b了常規(guī)的流,但是它是一種更有限的迭代器類(lèi)型(例如,您不能向后移動(dòng),這意味著并非以下所有算法都適用于流迭代器。

現(xiàn)在,考慮到這些迭代器類(lèi)型中的任何一種,我們可以使用所有旨在與迭代器一起使用的標(biāo)準(zhǔn)庫(kù)算法。 例如,要查找序列中值為IIterator的第一個(gè)元素:

std::find(first, last, 4); // return the first iterator which equals 4 and which is located in the interval [first, last)

或者我們可以對(duì)序列進(jìn)行排序(不適用于流迭代器):

std::sort(first, last);

或者如果我們編寫(xiě)一個(gè)將int平方的函數(shù),例如:

int square(int i) { return i * i; }

然后我們可以將其應(yīng)用于整個(gè)序列:

// for every element in the range [first, last), apply the square function, and output the result into the sequence starting with first

std::transform(first, last, first, square);

這就是迭代器的優(yōu)點(diǎn):它們抽象了容器的詳細(xì)信息,因此我們可以對(duì)任何序列應(yīng)用泛型操作。 多虧了迭代器,相同的IIterator或sort實(shí)施也可用于鏈接列表和數(shù)組,甚至可用于您自己的自制容器類(lèi)。

通用編程

泛型編程基本上是您的代碼應(yīng)盡可能通用的想法。 如上面的迭代器示例所示,我們提供了一組類(lèi)型必須支持的一組通用功能才能被稱為迭代器,然后編寫(xiě)適用于任何迭代器類(lèi)型的算法。

將此與傳統(tǒng)的面向?qū)ο缶幊踢M(jìn)行比較,在傳統(tǒng)的面向?qū)ο缶幊讨?#xff0c;迭代器必須通過(guò)繼承某種IIterator接口來(lái)“證明”它們是迭代器。 那將阻止我們使用原始指針作為迭代器,因此我們將失去通用性。

在C ++中,使用通用編程,我們不需要官方接口。 我們只是使用模板編寫(xiě)算法,因此它們接受恰好看起來(lái)像迭代器的任何類(lèi)型,而不管它們?cè)诤翁?#xff0c;何時(shí)以及如何定義,以及它們是否從公共基類(lèi)或接口派生。

總結(jié)

以上是生活随笔為你收集整理的c 是泛型程序设计语言,c ++中的“泛型编程”是什么意思?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。