一篇文章徹底搞懂C++常見容器
1.概述
C++容器屬于STL(標(biāo)準(zhǔn)模板庫(kù))中的一部分(六大組件之一),從字面意思理解,生活中的容器用來(lái)存放(容納)水或者食物,東西,而C++中的容器用來(lái)存放各種各樣的數(shù)據(jù),不同的容器具有不同的特性,下圖(思維導(dǎo)圖)中列舉除了常見的幾種C++容器,而這部分C++的容器與python中的序列有很多相似之處,也許這也很好地印證了江湖上“C生萬(wàn)物”的說(shuō)法。因本人是學(xué)完python后才學(xué)C++的,突然有種:“山重水復(fù)疑無(wú)路,柳暗花明又一村”的感覺。因?yàn)閜ython是偏向于頂層的語(yǔ)言,那時(shí)候什么迭代器,生成器之類的東西都不是非常清楚,然后在C++中又遇到了類似內(nèi)容,便有了更好的理解,也許這就是很多人不建議初學(xué)者學(xué)習(xí)python的原因吧。
2.容器詳解
2.1vector(向量)
從這個(gè)命名就可以很好地理解,在線性代數(shù)中,向量是一維的結(jié)構(gòu),而在容器中,向量也是看似一維的存儲(chǔ)形式??梢岳斫鉃殚L(zhǎng)度可變的數(shù)組。只不過(guò)在尾部增刪數(shù)據(jù)的時(shí)候效率最高,其他位置增刪數(shù)據(jù)則效率較低。舉個(gè)例子(開胃菜):
#include <iostream> #include <vector> using namespace std; // 程序的主函數(shù) int main() { vector<int> V; V.push_back(1); V.push_back(2); V.push_back(1); V.push_back(2); cout << V[0] << endl; system("pause"); return 0; }
打印輸出:1
。
從上面的例子可以看出,向量和數(shù)組的用法極其類似。當(dāng)然,容器還有一個(gè)極其好用的功能,就是容器的嵌套使用。
#include <iostream> #include <vector> using namespace std; // 程序的主函數(shù) int main() { vector<vector<int>> V; vector<int> sub_V; sub_V.push_back(1); sub_V.push_back(2); sub_V.push_back(1); V.push_back(sub_V); cout << V[0][1] << endl; system("pause"); return 0; }
打印輸出2
這個(gè)時(shí)候的向量可以看作是一個(gè)二維數(shù)組,當(dāng)然比二維數(shù)組更加靈活、強(qiáng)大。
當(dāng)然向量容器還有其他更加豐富的操作。比如:
int size = vec1.size(); //元素個(gè)數(shù) bool isEmpty = vec1.empty(); //判斷是否為空 vec1.insert(vec1.end(),5,3); //從vec1.back位置插入5個(gè)值為3的元素 vec1.pop_back(); //刪除末尾元素 vec1.erase(vec1.begin(),vec1.end());//刪除之間的元素,其他元素前移 cout<<(vec1==vec2)?true:false; //判斷是否相等==、!=、>=、<=... vector<int>::iterator iter = vec1.begin(); //獲取迭代器首地址 vector<int>::const_iterator c_iter = vec1.begin(); //獲取const類型迭代器 vec1.clear(); //清空元素
舉個(gè)最常見的例子:
#include <iostream> #include <vector> using namespace std; // 程序的主函數(shù) int main() { vector<int> V; V.push_back(1); V.push_back(2); V.push_back(3); for (vector<int>::iterator it = V.begin(); it != V.end(); it++) cout << *it << " "; cout << endl; cout << "==========================" << endl; V.insert(V.begin() + 2,10); for (vector<int>::iterator it = V.begin(); it != V.end(); it++) cout << *it << " "; system("pause"); return 0; }
注意如果不是在其尾部插入數(shù)據(jù),要傳入插入位置的迭代器。
打印輸出:
2.2deque(雙端隊(duì)列)
deque,顧名思義,從前后兩端都可以進(jìn)行數(shù)據(jù)的插入和刪除操作,同時(shí)支持?jǐn)?shù)據(jù)的快速隨機(jī)訪問(wèn)。舉個(gè)例子:
#include <iostream> #include <deque> using namespace std; // 程序的主函數(shù) int main() { deque<int> D; D.push_back(1); D.push_back(2); D.push_back(3); for (deque<int>::iterator it = D.begin(); it != D.end(); it++) cout << *it << " "; cout << endl; cout << "============在其索引2的位置插入10:" << endl; D.insert(D.begin() + 2,10); for (deque<int>::iterator it = D.begin(); it != D.end(); it++) cout << *it << " "; cout << endl; cout << "============在其頭部插入0:" << endl; D.push_front(0); for (deque<int>::iterator it = D.begin(); it != D.end(); it++) cout << *it << " "; cout << endl; cout << "============在其頭部彈出0:" << endl; D.pop_front(); for (deque<int>::iterator it = D.begin(); it != D.end(); it++) cout << *it << " "; system("pause"); return 0; }
打印輸出:
2.3list(列表)
列表是用雙向鏈表實(shí)現(xiàn)的,所謂的雙向鏈表,指的是既可以從鏈表的頭部開始搜索找到鏈表的尾部,也可以進(jìn)行反向搜索,從尾部到頭部。這使得list在任何位置插入和刪除元素都變得非常高效,但是隨機(jī)訪問(wèn)速度變得非常慢,因?yàn)楸4娴牡刂肥遣贿B續(xù)的,所以list沒有重載[]
運(yùn)算符,也就是說(shuō),訪問(wèn)list元素的時(shí)候,再也不像向量和雙端隊(duì)列那么方便,不可以像我們以前在C語(yǔ)言的時(shí)候,訪問(wèn)數(shù)組那樣對(duì)其元素進(jìn)行訪問(wèn)。
一起來(lái)看個(gè)例子:
#include <iostream> #include <list> using namespace std; // 程序的主函數(shù) int main() { //list的創(chuàng)建和初始化 list<int> lst1; //創(chuàng)建空l(shuí)ist list<int> lst2(3); //創(chuàng)建含有三個(gè)元素的list list<int> lst3(3, 2); //創(chuàng)建含有三個(gè)元素的值為2的list list<int> lst4(lst3); //使用lst3初始化lst4 list<int> lst5(lst3.begin(), lst3.end()); //同lst4 cout << "lst4中的元素有:" << endl; for (list<int>::iterator it = lst4.begin(); it != lst4.end(); it++) cout << *it << " "; cout << endl; cout << "lst5中的元素有:" << endl; for (list<int>::iterator it = lst5.begin(); it != lst5.end(); it++) cout << *it << " "; cout << endl; system("pause"); return 0; }
運(yùn)行,打印輸出:
然后再來(lái)看一個(gè)元素的添加,排序的例子。
#include <iostream> #include <list> #include <vector> using namespace std; // 程序的主函數(shù) int main() { //list的創(chuàng)建和初始化 list<int> lst1; //創(chuàng)建空l(shuí)ist for(int i = 0; i < 10; i++) lst1.push_back(9-i); //添加值 cout << "lst1中的元素有:" << endl; for (list<int>::iterator it = lst1.begin(); it != lst1.end(); it++) cout << *it << " "; cout << endl; cout << "對(duì)lst1中的元素進(jìn)行排序:" << endl; lst1.sort(); for (list<int>::iterator it = lst1.begin(); it != lst1.end(); it++) cout << *it << " "; cout << endl; cout << "在索引為5的地方插入999:" << endl; list<int>::iterator insert_it = lst1.begin(); for (int i = 0; i < 5; i++) insert_it++; lst1.insert(insert_it, 3, 999); for (list<int>::iterator it = lst1.begin(); it != lst1.end(); it++) cout << *it << " "; cout << endl; cout << "刪除相鄰重復(fù)元素后:" << endl; lst1.unique(); //刪除相鄰重復(fù)元素 for (list<int>::iterator it = lst1.begin(); it != lst1.end(); it++) cout << *it << " "; cout << endl; system("pause"); return 0; }
運(yùn)行后,打印輸出:
特別注意,由于list的底層是雙向鏈表,因此insert操作無(wú)法直接像向量和雙端隊(duì)列一樣直接插入數(shù)據(jù),只能通過(guò)迭代器的自加移動(dòng)到相應(yīng)位置,再插入數(shù)據(jù)。
2.4 array(數(shù)組)
array和C語(yǔ)言中的數(shù)組沒有太大的區(qū)別,建立后只能存儲(chǔ)一種類型的數(shù)據(jù),且不能改變大小。比較簡(jiǎn)單,舉個(gè)例子:
#include <iostream> #include <string> #include <array> using namespace std; // 程序的主函數(shù) int main() { array<int, 4> arr = {1, 3, 2}; cout << "arr values:" << std::endl; for (array<int, 4>::iterator it = arr.begin(); it != arr.end(); it++) { cout << *it << " "; } cout << endl; cout << "sizeof(arr) = " << sizeof(arr) << endl; cout << "size of arr = " << arr.size() << endl; cout << "max size arr = " << arr.max_size() << endl; cout << "empty = " << (arr.empty() ? "no" : "yes") << endl; system("pause"); return 0; }
當(dāng)然,最常見的,array也支持嵌套,可以采用這樣的方式來(lái)構(gòu)建二維(多維)數(shù)組,由于比較簡(jiǎn)單,就不舉例了。
2.5 string(字符串)
與vector相似的容器。專門用于保存字符。隨機(jī)訪問(wèn)快。尾部插入刪除快。在部分說(shuō)法中,string不算是STL容器,但是為了內(nèi)容的完整性,我們還是將其一并學(xué)習(xí)。
#include <iostream> #include <string> using namespace std; // 程序的主函數(shù) int main() { string s1 = "Bob:"; string s2("hellow world!"); for (int i = 0; i < s1.size(); i++) { cout << s1[i]; } cout << endl; for (int i = 0; i < s2.size(); i++) { cout << s2[i]; } cout << endl; cout << s1 + s2 << endl; s1.insert(s1.size(),"you say "); cout << s1 + s2 << endl; system("pause"); return 0; }
運(yùn)行,打印輸出如下:
通過(guò)以上例子可以發(fā)現(xiàn),與我們?cè)贑語(yǔ)言中學(xué)習(xí)的string并沒有多少區(qū)別,其實(shí)本身區(qū)別也不是很大,只是在創(chuàng)建了之后還可以添加元素(盲猜是新創(chuàng)建了一個(gè)同名的string,僅此而已),且添加元素的方式也很簡(jiǎn)單,直接通過(guò)insert(插入位置,需要添加的字符串)
這樣的格式添加即可。上面一個(gè)例子是從末尾添加的,所以索引肯定是s1.size()
。當(dāng)然還有字符串的相加,字符串的比較等,都是屬于更為基礎(chǔ)的內(nèi)容,沒有添加到例子當(dāng)中去,感興趣的同學(xué)可以自己找資料去學(xué)習(xí)。
2.6 map(映射)
map容器和python中的字典非常類似,或者說(shuō)一模一樣。都是通過(guò)鍵值對(duì)的方式來(lái)存儲(chǔ)和訪問(wèn)數(shù)據(jù)的,底層是通過(guò)紅黑樹來(lái)實(shí)現(xiàn)的。先來(lái)看個(gè)map的創(chuàng)建以及初始化的例子。
#include <iostream> #include <map> #include <string> using namespace std; // 程序的主函數(shù) int main() { //map的創(chuàng)建和初始化 //第一種:用insert函數(shù)插入pair數(shù)據(jù): map<int, string> my_map; my_map.insert(pair<int, string>(1, "first")); my_map.insert(pair<int, string>(2, "second")); //第二種:用insert函數(shù)插入value_type數(shù)據(jù): my_map.insert(map<int, string>::value_type(3, "first")); my_map.insert(map<int, string>::value_type(4, "second")); //第三種:用數(shù)組的方式直接賦值: my_map[5] = "first"; my_map[6] = "second"; map<int, string>::iterator it; //迭代器遍歷 for (it = my_map.begin(); it != my_map.end(); it++) cout << it->first << "->" <<it->second << endl; system("pause"); return 0; }
運(yùn)行,打印輸出如下結(jié)果:
從以上結(jié)果可以看出,其中數(shù)組直接賦值的方法最簡(jiǎn)單直接,最容易理解。當(dāng)然map保存的是鍵值對(duì),所以前面的int類型數(shù)據(jù)(key)并不代表其位置。比方說(shuō),我們將其中的int修改為float也是可以的。代碼如下:
#include <iostream> #include <map> #include <string> using namespace std; // 程序的主函數(shù) int main() { //map的創(chuàng)建和初始化 //第一種:用insert函數(shù)插入pair數(shù)據(jù): map<float, string> my_map; my_map.insert(pair<float, string>(1, "first")); my_map.insert(pair<float, string>(2, "second")); //第二種:用insert函數(shù)插入value_type數(shù)據(jù): my_map.insert(map<float, string>::value_type(3, "first")); my_map.insert(map<float, string>::value_type(4, "second")); //第三種:用數(shù)組的方式直接賦值: my_map[5.3] = "first"; my_map[6.6] = "second"; map<float, string>::iterator it; //迭代器遍歷 for (it = my_map.begin(); it != my_map.end(); it++) cout << it->first << "->" <<it->second << endl; system("pause"); return 0; }
當(dāng)然,同其他的容器類型一樣,map同樣支持嵌套,比如:
#include <iostream> #include <map> #include <string> using namespace std; // 程序的主函數(shù) int main() { //map的嵌套用法 map<int,map<int,string>> my_map; my_map[1][1] = "張三"; my_map[1][2] = "李四"; my_map[1][3] = "王五"; for (map<int, map<int, string>>::iterator it = my_map.begin(); it != my_map.end(); it++) { for (map<int, string>::iterator in_it = it->second.begin(); in_it != it->second.end(); in_it++) { cout << it->first << "年級(jí)" << in_it->first << "號(hào)同學(xué):" << in_it->second << endl; } } cout << endl; system("pause"); return 0; }
運(yùn)行,打印輸出如下:
還有一個(gè)很重要的問(wèn)題,就是map元素的刪除。map元素的刪除有好多種方法,下面僅僅列舉 常見幾種。
#include <iostream> #include <map> #include <string> using namespace std; void printMap(const map<string, int>& students) { for (auto ii = students.begin(); ii != students.end(); ii++) { cout << "姓名:" << ii->first << " \t詩(shī)作: " << ii->second << "篇" << endl; } cout << endl; } int main(int argc, char* argv[]) { map<string, int> students; students["李白"] = 346; students["杜甫"] = 300; students["王維"] = 200; students["李商隱"] = 113; students["杜牧"] = 156; cout << "原map:" << endl; printMap(students); students.erase("李白"); cout << "刪除 李白 后:" << endl; printMap(students); students.erase(std::begin(students)); cout << "刪除第一個(gè)元素后:" << endl; printMap(students); map<string, int>::iterator iter = students.find("杜牧"); students.erase(iter); cout << "刪除杜牧后:" << endl; printMap(students); system("pause"); return 0; }
運(yùn)行后,打印輸出:
從上面的例子也可以看出,map中的鍵值對(duì)不一定是按照我們創(chuàng)建的順序保存數(shù)據(jù),map會(huì)按照key的值內(nèi)部進(jìn)行排序,但是保持其鍵值對(duì)的對(duì)應(yīng)關(guān)系不變。
2.7 set(集合)
set也是一種關(guān)聯(lián)性容器,它同map一樣,底層使用紅黑樹實(shí)現(xiàn),插入刪除操作時(shí)僅僅移動(dòng)指針即可,不涉及內(nèi)存的移動(dòng)和拷貝,所以效率比較高。從中文名就可以明顯地看出,在set中不會(huì)存在重復(fù)的元素,若是保存相同的元素,將直接視為無(wú)效,我們先來(lái)看個(gè)簡(jiǎn)單的例子(關(guān)于set的創(chuàng)建和元素的添加等):
#include <iostream> #include <set> #include <vector> using namespace std; // 程序的主函數(shù) int main() { vector<int> ivec; for (vector<int>::size_type i = 0; i != 10; i++) { ivec.push_back(i); ivec.push_back(i); } set<int> iset(ivec.begin(), ivec.end()); cout << "向量中的元素為:" << endl; for (vector<int>::iterator it = ivec.begin(); it != ivec.end(); it++) { cout << *it << " "; } cout << endl; cout << "集合中的元素為:" << endl; for (set<int>::iterator it = iset.begin(); it != iset.end(); it++) { cout << *it << " "; } cout << endl; cout << "向量的大小為:" << endl; cout << ivec.size() << endl; cout << "集合的大小為:" << endl; cout << iset.size() << endl; system("pause"); return 0; }
打印輸出:
上面例子的方法,相當(dāng)于直接將向量的值賦給了集合,從而順便創(chuàng)建了集合,那么如果想通過(guò)逐一賦值的方式創(chuàng)建集合,又該如何編寫代碼呢?如何清除集合中的元素呢?以及是否知道某元素在集合中呢?同樣我們通過(guò)一段代碼來(lái)看一下。
#include <iostream> #include <set> #include <vector> #include <string> using namespace std; // 程序的主函數(shù) int main() { set<string> set1; set1.insert("the"); //刪除集合 while (!set1.empty()) { //獲取頭部 set<string>::iterator it = set1.begin(); //打印頭部元素 cout << *it << endl; //從頭部刪除元素 set1.erase(set1.begin()); } set<int>set2; for (int i = 100; i < 110; i++) set2.insert(i); cout << "set2中5出現(xiàn)的次數(shù)為:"; cout << set2.count(5) << endl; set2.clear(); cout << "set2清除之后的大小為:"; cout << set2.size() << endl; system("pause"); return 0; }
運(yùn)行,打印輸出:
通過(guò)以上的例子可以發(fā)現(xiàn),set可以直接通過(guò)insert()
方法添加數(shù)據(jù),而數(shù)據(jù)內(nèi)部是自動(dòng)排序的,所以不用擔(dān)心數(shù)據(jù)的順序問(wèn)題,當(dāng)然也可以像map那樣,通過(guò)迭代器添加到指定位置,查詢set中有無(wú)該數(shù)據(jù)可以直接使用count()
方法,有則返回1
,無(wú)則返回0
。
3.后記
到此這篇關(guān)于徹底搞懂C++常見容器的文章就介紹到這了,更多相關(guān)C++常見容器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解C++中的vector容器及用迭代器訪問(wèn)vector的方法
使用迭代器iterator可以更方便地解引用和訪問(wèn)成員,當(dāng)然也包括vector中的元素,本文就來(lái)詳解C++中的vector容器及用迭代器訪問(wèn)vector的方法,需要的朋友可以參考下2016-05-05C++任意線程通過(guò)hwnd實(shí)現(xiàn)將操作發(fā)送到UI線程執(zhí)行
做Windows界面開發(fā)時(shí),經(jīng)常需要在多線程環(huán)境中將操作拋到主線程執(zhí)行,下面我們就來(lái)學(xué)習(xí)一下如何在不需要重新定義消息以及接收消息的情況下實(shí)現(xiàn)這一要求,感興趣的可以了解下2024-03-03C++實(shí)現(xiàn)LeetCode(98.驗(yàn)證二叉搜索樹)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(98.驗(yàn)證二叉搜索樹),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07解決C語(yǔ)言輸入單個(gè)字符屏蔽回車符的問(wèn)題
這篇文章主要介紹了解決C語(yǔ)言輸入單個(gè)字符屏蔽回車符的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的推箱子游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的推箱子游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01C語(yǔ)言深入講解棧與堆和靜態(tài)存儲(chǔ)區(qū)的使用
對(duì)大多數(shù)C 語(yǔ)言初學(xué)者來(lái)說(shuō),堆棧卻是一個(gè)很模糊的概念。堆棧是一種數(shù)據(jù)結(jié)構(gòu),一個(gè)在程序運(yùn)行時(shí)用于存放的地方,相信這可能是很多初學(xué)者共同的認(rèn)識(shí),靜態(tài)存儲(chǔ)區(qū)即內(nèi)存在程序編譯的時(shí)候就已經(jīng)分配好,這塊內(nèi)存在程序的整個(gè)運(yùn)行期間都存在2022-04-04C語(yǔ)言?八大排序算法的過(guò)程圖解及實(shí)現(xiàn)代碼
排序是數(shù)據(jù)結(jié)構(gòu)中很重要的一章,本文主要為大家介紹了常用的八個(gè)排序算法(插入,希爾,選擇,堆排,冒泡,快排,歸并,計(jì)數(shù))的過(guò)程及代碼實(shí)現(xiàn),需要的朋友可以參考一下2021-12-12VS2019配置OpenCV時(shí)找不到Microsoft.Cpp.x64.user的解決方法
這篇文章主要介紹了VS2019配置OpenCV時(shí)找不到Microsoft.Cpp.x64.user的解決方法,需要的朋友可以參考下2020-02-02