STL中的容器:
deque容器的常用介面及用法:
deque:它被稱作雙端陣列,可以在頭部和尾部插入或洗掉資料;
deque 容器和 vector 容器的區別:
1.vector 容器對頭部插入、洗掉資料的效率較低,因為 vector 容器是單端陣列,若要從頭部插入或洗掉,得把后面的資料都往后挪或往前挪,因此資料量越大,則其時間效率越低;
2.deque 容器相對 vector 容器而言,它對于頭部插入資料或頭部洗掉資料的效率就高多了,這與它的內部實作相關;
3.vector 容器訪問單個資料的效率要高于 deque 容器,原因也是與 deque 容器的內部實作有關;

圖片轉自于黑馬程式員,是在學習的程序中截圖下來的
deque 容器內部作業原理:
1.deque 內部有個中控器,它維護著每段緩沖區中的內容,而緩沖區里面放著真實的資料;
2.中控器維護的其實是緩沖區的地址,使得使用 deque 容器時像一段連續的記憶體空間;
3.緩沖區的頭和尾是沒有插滿資料的,因此可以繼續添加,添加滿后,則會開辟一塊新的緩沖區,中控器則記錄下新的緩沖區的地址;
4.由于中控器維護的是地址,因此當我們訪問單個元素時,內部的實作是從地址再轉到緩沖區,這里的時間效率就低于 vector 容器了;

圖片轉自于黑馬程式員,是在學習的程序中截圖下來的
各種函式介面具體如何使用,下面的代碼塊中會有詳細的使用方法
deque 容器建構式:
1.deque<T> d; 默認(無參)構造;
2.deque(const deque & d); 拷貝建構式
3.deque(begin,end); 把區間 [begin,end) 之間的資料拷貝到新創建的 deque 容器;
4.deque(n,elem); 把n個 elem 拷貝給新創建的 deque 容器;
#include <iostream>
#include <deque> //使用STL中的容器,得包含它的頭檔案
#include <algorithm> //使用STL提供的演算法,得包含它的頭檔案
using namespace std;
void printDeque(const deque<int> & d) //限制傳進來的deque容器只讀的狀態
{
for(deque<int>::const_iterator it=d.begin();it!=d.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
void test_1() //deque容器建構式
{
int i = 0;
deque<int> d1; //默認(無參)建構式
for(i=0;i<10;i++)
{
d1.push_back(i+1);
}
printDeque(d1);
deque<int> d2(d1); //拷貝建構式
printDeque(d2);
deque<int> d3(d1.begin(),d1.end()); //把區間[d1.begin(),d1.end())之間的資料拷貝到新創建的deque容器
printDeque(d3);
deque<int> d4(10,1); //把10個1拷貝到新創建的deque容器
printDeque(d4);
}
int main()
{
test_1();
system("pause");
return 0;
}
deque 容器賦值:
1.deque& operator=(const deque & d); 通過多載賦值運算子的方式給新創建的 deque 容器賦值;
2.assign(begin,end); 通過成員函式 assign() 把 [begin,end) 之間的資料給新創建的 deque 容器賦值;
3.assign(n,elem); 通過成員函式 assign() 把n個 elem 給新創建的 deque 容器賦值;
#include <iostream>
#include <deque> //使用STL中的容器,得包含它的頭檔案
#include <algorithm> //使用STL提供的演算法,得包含它的頭檔案
using namespace std;
void printDeque(const deque<int> & d) 限制傳進來的deque容器只讀的狀態
{
for(deque<int>::const_iterator it=d.begin();it!=d.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
void test_1() //deque容器賦值
{
int i = 0;
deque<int> d1; //默認(無參)建構式
for(i=0;i<10;i++)
{
d1.push_back(i+1);
}
printDeque(d1);
deque<int> d2;
d2 = d1; //通過多載賦值運算子的方式給新創建的deque容器賦值
printDeque(d2);
deque<int> d3;
d3.assign(d1.begin(),d1.end()); //通過成員函式assign()把[begin,end)之間的資料給新創建的deque容器賦值
printDeque(d3);
deque<int> d4;
d4.assign(10,1); //通過成員函式assign()把10個1給新創建的deque容器賦值
printDeque(d4);
}
int main()
{
test_1();
system("pause");
return 0;
}
deque 容器的大小:
1.empty(); 判斷 deque 容器是否為空,如果 deque 容器為空,則回傳 true,否則回傳 false;
2.size() 用于查看容器的大小(元素個數);
3.resize(int num); 重新指定容器的大小為 num,若容器擴大了,則以默認值(0)來填充,若容器變小了,則洗掉多出來的部分;
4.resize(int num,int elem);
重新指定容器的大小為 num,若容器擴大了,則以 elem 來填充多出來的部分,若容器變小了,則洗掉多出來的部分;
注意:由于 deque 內部作業原理的特殊性,deque 容器沒有容量的概念;
#include <iostream>
#include <deque> //使用STL中的容器,得包含它的頭檔案
#include <algorithm> //使用STL提供的演算法,得包含它的頭檔案
using namespace std;
void printDeque(const deque<int> & d)
{
for(deque<int>::const_iterator it=d.begin();it!=d.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
void test_1() //deque容器的大小
{
int i = 0;
deque<int> d1;
for(i=0;i<10;i++)
{
d1.push_back(i+1);
}
printDeque(d1);
if(d1.empty()) //判斷deque容器是否為空,如果deque容器為空,則回傳true,否則回傳false
{
cout << "當前容器為空" << endl;
}
else
{
cout << "當前容器不為空" << endl;
cout << "d1的大小為:" << d1.size() << endl; //用于查看容器的大小(元素個數)
}
d1.resize(20); //重新指定容器的大小為20,容器擴大,以默認值(0)來填充
printDeque(d1);
d1.resize(25,10); //重新指定容器的大小為25,容器擴大,以10來填充
printDeque(d1);
d1.resize(5); //重新指定容器的大小為5,容器縮小,洗掉多出來的部分
printDeque(d1);
}
int main()
{
test_1();
system("pause");
return 0;
}
deque 容器插入和洗掉:
兩端插入和洗掉:
1.push_back(elem); 在 deque 容器的尾部插入資料;
2.push_front(elem); 在 deque 容器的頭部插入資料;
3.pop_back(); 在 deque 容器的尾部洗掉資料;
4.pop_front(); 在 deque 容器的頭部洗掉資料;
指定位置的插入和洗掉:
1.insert(pos,elem); 通過迭代器在 deque 容器的指定位置插入一個 elem 元素;
2.insert(pos,n,elem); 通過迭代器在 deque 容器的指定位置插入n個 elem 元素;
3.insert(pos,begin,end); 通過迭代器在 deque 容器的指定位置插入區間 [begin,end) 的資料;
4.erase(pos); 通過迭代器洗掉掉 deque 容器指定位置的資料;
5.erase(begin,end); 通過迭代器洗掉掉 deque 容器區間 [begin,end) 之間的資料;
6.clear(); 清空當前的 deque 容器;
#include <iostream>
#include <deque> //使用STL中的容器,得包含它的頭檔案
#include <algorithm> //使用STL提供的演算法,得包含它的頭檔案
using namespace std;
void printDeque(const deque<int> & d)
{
for(deque<int>::const_iterator it=d.begin();it!=d.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
void test_1() //deque容器兩端插入和洗掉
{
deque<int> d1;
//尾插法
d1.push_back(10);
d1.push_back(20);
//頭插法
d1.push_front(100);
d1.push_front(200);
printDeque(d1);
//尾刪法
d1.pop_back();
//頭刪法
d1.pop_front();
printDeque(d1);
}
void test_2() //deque容器指定位置的插入和洗掉
{
deque<int> d1;
d1.push_back(10);
d1.push_back(20);
d1.push_front(100);
d1.push_front(200);
d1.insert(d1.begin(),5000); //通過迭代器在deque容器的頭部插入一個5000
printDeque(d1);
d1.insert(d1.end(),2,5000); //通過迭代器在deque容器的尾部部插入兩個5000
printDeque(d1);
deque<int> d2;
d2.push_back(1);
d2.push_back(2);
d2.push_back(3);
d1.insert(d1.begin(),d2.begin(),d2.end()); //通過迭代器在d1的頭部插入d2區間[d2.begin(),d2.end())的資料
printDeque(d1);
d1.erase(d1.begin()); //通過迭代器洗掉掉deque容器頭部的資料
printDeque(d1);
deque<int>::iterator it = d2.begin();
it++;
d2.erase(it,d2.end()); //通過迭代器洗掉掉deque容器區間[it,end)之間的資料
printDeque(d2);
d1.clear(); //清空當前的deque容器
printDeque(d1);
}
int main()
{
test_1();
test_2();
system("pause");
return 0;
}
deque 容器資料存取:
1.operator[]; 通過多載 [] 的方式,獲取 deque 容器的每一個元素;
2.at(int idx); 通過成員函式 at() 獲取 deque 容器的每一個元素;
3.front(); 回傳容器中的第一個元素;
4.back(); 回傳容器中的最后一個元素;
#include <iostream>
#include <deque> //使用STL中的容器,得包含它的頭檔案
#include <algorithm> //使用STL提供的演算法,得包含它的頭檔案
using namespace std;
void test_1() //deque容器資料存取
{
int i = 0;
deque<int> d1;
//300 200 100 10 20 30
d1.push_back(10);
d1.push_back(20);
d1.push_back(30);
d1.push_front(100);
d1.push_front(200);
d1.push_front(300);
for(i=0;i<d1.size();i++)
{
cout << d1[i] << " "; //通過多載[]的方式,獲取deque容器的每一個元素
}
cout << endl;
for(i=0;i<d1.size();i++)
{
cout << d1.at(i) << " "; // 通過成員函式at()獲取deque容器的每一個元素
}
cout << endl;
cout << "第一個元素為:" << d1.front() << endl; //回傳容器中的第一個元素
cout << "最后一個元素為:" << d1.back() << endl; //回傳容器中的最后一個元素
}
int main()
{
test_1();
system("pause");
return 0;
}
deque 容器排序:
1.sort(iterator begin,iterator end); 對區間 [begin,end) 之間的元素進行排序;(默認為從小到大,即升序)
注意:對于支持隨機訪問的迭代器,都可以直接利用 sort() 排序進行排序(sort()演算法其實是 STL 中很常用的排序演算法,STL 中的常用演算法后續也會更新出來(●’?’●));
這里提一嘴,STL中的常用演算法只允許擁有 支持隨機訪問的迭代器 的容器使用,迭代器不支持隨機訪問 的容器是不可以使用這些常用演算法的,但為了解決這樣的問題,這些容器內置了一些成員函式,這些成員函式的功能與常用演算法一致,這些成員函式就只能由對應的容器來使用了;
#include <iostream>
#include <deque> //使用STL中的容器,得包含它的頭檔案
#include <algorithm> //使用STL提供的演算法,得包含它的頭檔案
using namespace std;
void printDeque(const deque<int> & d)
{
for(deque<int>::const_iterator it=d.begin();it!=d.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
void test_1() //deque容器排序
{
int i = 0;
deque<int> d1;
//300 200 100 10 20 30
d1.push_back(10);
d1.push_back(20);
d1.push_back(30);
d1.push_front(100);
d1.push_front(200);
d1.push_front(300);
cout << "排序前:" << endl;
printDeque(d1);
sort(d1.begin(),d1.end()); //對于支持隨機訪問的迭代器,都可以直接利用sort排序進行排序
cout << "排序后:" << endl;
printDeque(d1);
}
int main()
{
test_1();
system("pause");
return 0;
}
以上就是STL中deque容器的一些常用介面和用法啦O(∩_∩)O,筆記中有錯誤的地方,歡迎指出,歡迎大家討論!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/272841.html
標籤:其他
上一篇:網站下方著作權資訊的正規寫法
