一、deque工作原理:
deque容器內(nèi)部有個(gè)中控器,維護(hù)每段緩沖區(qū)中的內(nèi)容,緩沖區(qū)中存放真實(shí)數(shù)據(jù);中控器維護(hù)的每個(gè)緩沖區(qū)的地址,使得使用deque時(shí)像一片連續(xù)的內(nèi)存空間,如下圖所示:
二、deque構(gòu)造函數(shù):
1、功能描述:
deque容器構(gòu)造
2、函數(shù)原型:
dequedeqT;//默認(rèn)構(gòu)造函數(shù)
deque(beg,end);//構(gòu)造函數(shù)將[beg,end)區(qū)間中的元素拷貝給本身
deque(n,elem);//構(gòu)造函數(shù)將n個(gè)elem拷貝給本身
deque(const deque &deq);//拷貝構(gòu)造函數(shù)
代碼應(yīng)用:
#include
結(jié)果輸出:
root@txp-virtual-machine:/home/txp/test2# ./a.out
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100
三、deque賦值操作:
1、功能描述:
給deque容器進(jìn)行賦值
2、函數(shù)原型:
deque operator=(const deque &deq);//重載等號(hào)操作符
assign(beg,end);//將[beg,end)區(qū)間中的數(shù)據(jù)拷貝賦值給本身
assign(n,elem);//將n個(gè)elem拷貝賦值給本身
代碼應(yīng)用:
#include
結(jié)果輸出:
root@txp-virtual-machine:/home/txp/test2# ./a.out
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
100 100 100 100 100 100 100 100 100 100
四、deque大小操作:
1、功能描述:
對(duì)deque容器的大小進(jìn)行操作
2、函數(shù)原型:
deque.empty();//判斷容器是否為空
deque.size();//返回容器中的元素個(gè)數(shù)
deque.resize(num);//重新指定容器的長(zhǎng)度num,若容器變長(zhǎng),則以默認(rèn)值填充新位置;如果容器變短,則末尾超出容器長(zhǎng)度的元素被刪除
deque.resize(num,elem);//重新指定容器的長(zhǎng)度num,若容器變長(zhǎng),則以elem值填充新位置;如果容器變短,則末尾超出容器長(zhǎng)度的元素被刪除
代碼應(yīng)用:
#include
結(jié)果輸出:
root@txp-virtual-machine:/home/txp/test2# ./a.out
0 1 2 3 4 5 6 7 8 9
d1 is not empty
d1 is : 10
0 1 2 3 4 5 6 7 8 9 1 1 1 1 1
0 1 2 3 4
注:deque沒有容量的概念!
五、deque插入和刪除:
1、功能描述:向deque容器中插入和刪除數(shù)據(jù)
2、函數(shù)原型:
(1)兩端插入操作:push_back(elem);//在容器尾部添加一個(gè)數(shù)據(jù)
push_front(elem);//在容器頭部插入一個(gè)數(shù)據(jù)
pop_back();//刪除容器最后一個(gè)元素?cái)?shù)據(jù)
pop_front();//刪除容器第一數(shù)據(jù)
(2)指定位置操作:
insert(pos,elem);//在pos位置插入一個(gè)elem元素的拷貝,返回新數(shù)據(jù)的位置
insert(pos,n,elem);//在pos位置插入n個(gè)elem數(shù)據(jù),無返回值
insert(pos,beg,end);//在pos位置插入[beg,end)區(qū)間的數(shù)據(jù),無返回值
clear();//清空容器的所有數(shù)據(jù)
erase(beg,end);//刪除[beg,end)區(qū)間的數(shù)據(jù),返回下一個(gè)數(shù)據(jù)的位置
erase(pos);//刪除pos位置的數(shù)據(jù),返回下一個(gè)數(shù)據(jù)的位置
兩端代碼應(yīng)用:
#include
結(jié)果輸出:
root@txp-virtual-machine:/home/txp/test2# ./a.out
200 100 10 20
200 100 10
100 10
代碼應(yīng)用:
#include
結(jié)果輸出:
root@txp-virtual-machine:/home/txp/test2# ./a.out
200 100 10 20
1000 200 100 10 20
10000 10000 1000 200 100 10 20
1 2 3 10000 10000 1000 200 100 10 20
代碼應(yīng)用:
#include
結(jié)果輸出:
root@txp-virtual-machine:/home/txp/test2# ./a.out
200 100 10 20
200 10 20
六、deque數(shù)據(jù)存取:
1、功能描述:
對(duì)deque容器中的數(shù)據(jù)存取操作
2、函數(shù)原型:
at(int idx);//返回索引idx所指的數(shù)據(jù)
operator[];//返回索引idx所指的數(shù)據(jù)
front();//返回容器中第一個(gè)元素
back();//返回容器中最后數(shù)據(jù)元素
代碼應(yīng)用:
#include
結(jié)果輸出:
root@txp-virtual-machine:/home/txp/test2# ./a.out
200 100 10 20
200 100 10 20
the first is : 200
the last is : 20
七、deque排序:
1、功能描述:
利用算法實(shí)現(xiàn)對(duì)deque容器進(jìn)行排序
2、算法:
sort(iterator beg,iterator end);//對(duì)beg和end區(qū)間元素進(jìn)行排序
代碼應(yīng)用:
#include
結(jié)果輸出:
root@txp-virtual-machine:/home/txp/test2# ./a.out
200 100 10 20
10 20 100 200
-
容器
+關(guān)注
關(guān)注
0文章
504瀏覽量
22319 -
可編程邏輯
+關(guān)注
關(guān)注
7文章
523瀏覽量
44491
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
三星在4nm邏輯芯片上實(shí)現(xiàn)40%以上的測(cè)試良率
Kubernetes:構(gòu)建高效的容器化應(yīng)用平臺(tái)
智慧路燈“照亮”城市夜空——晉中市城區(qū)4萬盞路燈實(shí)現(xiàn)節(jié)能化智慧化改造

三星硅電容器已完成量產(chǎn)準(zhǔn)備
群芯微電子受邀參加2024年韓國(guó)電子展
使用碼云實(shí)現(xiàn)CC3220空中升級(jí)

MT6701磁編碼IC將在工業(yè)4.0智能制造中的應(yīng)用

三星貼片電容器容量怎么換算?
MT6835磁編碼IC在工業(yè)生產(chǎn)中的液位檢測(cè)領(lǐng)域的應(yīng)用

鴻蒙語言基礎(chǔ)類庫(kù):ohos.util.Deque 線性容器Deque

全球首個(gè)星閃指向遙控,智慧家庭新紀(jì)元的引領(lǐng)者
新技術(shù)融合共生:5G、AI與云計(jì)算引領(lǐng)數(shù)字經(jīng)濟(jì)時(shí)代
安全是最大的豪華!星紀(jì)元ET成功挑戰(zhàn)31.9米空中墜落試驗(yàn)

評(píng)論