一、deque工作原理:
deque容器內(nèi)部有個中控器,維護(hù)每段緩沖區(qū)中的內(nèi)容,緩沖區(qū)中存放真實(shí)數(shù)據(jù);中控器維護(hù)的每個緩沖區(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個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);//重載等號操作符
assign(beg,end);//將[beg,end)區(qū)間中的數(shù)據(jù)拷貝賦值給本身
assign(n,elem);//將n個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、功能描述:
對deque容器的大小進(jìn)行操作
2、函數(shù)原型:
deque.empty();//判斷容器是否為空
deque.size();//返回容器中的元素個數(shù)
deque.resize(num);//重新指定容器的長度num,若容器變長,則以默認(rèn)值填充新位置;如果容器變短,則末尾超出容器長度的元素被刪除
deque.resize(num,elem);//重新指定容器的長度num,若容器變長,則以elem值填充新位置;如果容器變短,則末尾超出容器長度的元素被刪除
代碼應(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);//在容器尾部添加一個數(shù)據(jù)
push_front(elem);//在容器頭部插入一個數(shù)據(jù)
pop_back();//刪除容器最后一個元素?cái)?shù)據(jù)
pop_front();//刪除容器第一數(shù)據(jù)
(2)指定位置操作:
insert(pos,elem);//在pos位置插入一個elem元素的拷貝,返回新數(shù)據(jù)的位置
insert(pos,n,elem);//在pos位置插入n個elem數(shù)據(jù),無返回值
insert(pos,beg,end);//在pos位置插入[beg,end)區(qū)間的數(shù)據(jù),無返回值
clear();//清空容器的所有數(shù)據(jù)
erase(beg,end);//刪除[beg,end)區(qū)間的數(shù)據(jù),返回下一個數(shù)據(jù)的位置
erase(pos);//刪除pos位置的數(shù)據(jù),返回下一個數(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、功能描述:
對deque容器中的數(shù)據(jù)存取操作
2、函數(shù)原型:
at(int idx);//返回索引idx所指的數(shù)據(jù)
operator[];//返回索引idx所指的數(shù)據(jù)
front();//返回容器中第一個元素
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)對deque容器進(jìn)行排序
2、算法:
sort(iterator beg,iterator end);//對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文章
521瀏覽量
22799 -
可編程邏輯
+關(guān)注
關(guān)注
7文章
526瀏覽量
45295
發(fā)布評論請先 登錄
超級電容器與傳統(tǒng)電容器的區(qū)別
ADSS光纜:電力通信的“空中橋梁”
多層陶瓷電容器與超級電容器的區(qū)別
空中客車與愛立信成功部署5G專網(wǎng)解決方案
動態(tài) IP 工作室:解鎖數(shù)字時(shí)代的網(wǎng)絡(luò)奧秘
三星貼片電容器規(guī)格對照表的內(nèi)容概括
三星在4nm邏輯芯片上實(shí)現(xiàn)40%以上的測試良率
超級電容器均壓電路狀況與展望
《手把手教你做星閃無人機(jī)—KaihongOS星閃無人機(jī)開發(fā)實(shí)戰(zhàn)》系列課程課件匯總
三星車規(guī)級電容在電動車高壓DC-DC轉(zhuǎn)換器中的作用
使用HDDL卡運(yùn)行OpenVINO應(yīng)用程序容器或HDDL daemon容器時(shí)遇到錯誤,怎么解決?
Kubernetes:構(gòu)建高效的容器化應(yīng)用平臺
5266 LTE 空中接口監(jiān)測儀
容器引擎是什么意思
智慧路燈“照亮”城市夜空——晉中市城區(qū)4萬盞路燈實(shí)現(xiàn)節(jié)能化智慧化改造

夜空中最亮的星——deque容器
評論