亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

一篇文章徹底搞懂C++常見容器

 更新時間:2023年02月13日 16:38:15   作者:頭發(fā)夠用的程序員  
容器就是一些特定類型對象的集合,容器可以分為順序容器和關(guān)聯(lián)容器,下面這篇文章主要給大家介紹了關(guān)于C++常見容器的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下

1.概述

C++容器屬于STL(標準模板庫)中的一部分(六大組件之一),從字面意思理解,生活中的容器用來存放(容納)水或者食物,東西,而C++中的容器用來存放各種各樣的數(shù)據(jù),不同的容器具有不同的特性,下圖(思維導圖)中列舉除了常見的幾種C++容器,而這部分C++的容器與python中的序列有很多相似之處,也許這也很好地印證了江湖上“C生萬物”的說法。因本人是學完python后才學C++的,突然有種:“山重水復疑無路,柳暗花明又一村”的感覺。因為python是偏向于頂層的語言,那時候什么迭代器,生成器之類的東西都不是非常清楚,然后在C++中又遇到了類似內(nèi)容,便有了更好的理解,也許這就是很多人不建議初學者學習python的原因吧。

2.容器詳解

2.1vector(向量)

從這個命名就可以很好地理解,在線性代數(shù)中,向量是一維的結(jié)構(gòu),而在容器中,向量也是看似一維的存儲形式??梢岳斫鉃殚L度可變的數(shù)組。只不過在尾部增刪數(shù)據(jù)的時候效率最高,其他位置增刪數(shù)據(jù)則效率較低。舉個例子(開胃菜):

#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ù)組的用法極其類似。當然,容器還有一個極其好用的功能,就是容器的嵌套使用。

#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這個時候的向量可以看作是一個二維數(shù)組,當然比二維數(shù)組更加靈活、強大。

當然向量容器還有其他更加豐富的操作。比如:

    int size = vec1.size();         //元素個數(shù)
    bool isEmpty = vec1.empty();    //判斷是否為空
    vec1.insert(vec1.end(),5,3);    //從vec1.back位置插入5個值為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();                 //清空元素

舉個最常見的例子:

#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(雙端隊列)

deque,顧名思義,從前后兩端都可以進行數(shù)據(jù)的插入和刪除操作,同時支持數(shù)據(jù)的快速隨機訪問。舉個例子:

#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(列表)

列表是用雙向鏈表實現(xiàn)的,所謂的雙向鏈表,指的是既可以從鏈表的頭部開始搜索找到鏈表的尾部,也可以進行反向搜索,從尾部到頭部。這使得list在任何位置插入和刪除元素都變得非常高效,但是隨機訪問速度變得非常慢,因為保存的地址是不連續(xù)的,所以list沒有重載[]運算符,也就是說,訪問list元素的時候,再也不像向量和雙端隊列那么方便,不可以像我們以前在C語言的時候,訪問數(shù)組那樣對其元素進行訪問。
一起來看個例子:

#include <iostream> 
#include <list> 
using namespace std;
// 程序的主函數(shù)
int main()
{
	//list的創(chuàng)建和初始化
	list<int> lst1;          //創(chuàng)建空list

	list<int> lst2(3);       //創(chuàng)建含有三個元素的list

	list<int> lst3(3, 2); //創(chuàng)建含有三個元素的值為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;
}

運行,打印輸出:

然后再來看一個元素的添加,排序的例子。

#include <iostream> 
#include <list> 
#include <vector> 
using namespace std;
// 程序的主函數(shù)
int main()
{
	//list的創(chuàng)建和初始化
	list<int> lst1;          //創(chuàng)建空list

	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 << "對lst1中的元素進行排序:" << 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 << "刪除相鄰重復元素后:" << endl;
	lst1.unique();                         //刪除相鄰重復元素
	for (list<int>::iterator it = lst1.begin(); it != lst1.end(); it++)
		cout << *it << " ";
	cout << endl;
	system("pause");
	return 0;
}

運行后,打印輸出:

特別注意,由于list的底層是雙向鏈表,因此insert操作無法直接像向量和雙端隊列一樣直接插入數(shù)據(jù),只能通過迭代器的自加移動到相應位置,再插入數(shù)據(jù)。

2.4 array(數(shù)組)

array和C語言中的數(shù)組沒有太大的區(qū)別,建立后只能存儲一種類型的數(shù)據(jù),且不能改變大小。比較簡單,舉個例子:

#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;
}

當然,最常見的,array也支持嵌套,可以采用這樣的方式來構(gòu)建二維(多維)數(shù)組,由于比較簡單,就不舉例了。

2.5 string(字符串)

與vector相似的容器。專門用于保存字符。隨機訪問快。尾部插入刪除快。在部分說法中,string不算是STL容器,但是為了內(nèi)容的完整性,我們還是將其一并學習。

#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;
}

運行,打印輸出如下:

通過以上例子可以發(fā)現(xiàn),與我們在C語言中學習的string并沒有多少區(qū)別,其實本身區(qū)別也不是很大,只是在創(chuàng)建了之后還可以添加元素(盲猜是新創(chuàng)建了一個同名的string,僅此而已),且添加元素的方式也很簡單,直接通過insert(插入位置,需要添加的字符串)這樣的格式添加即可。上面一個例子是從末尾添加的,所以索引肯定是s1.size()。當然還有字符串的相加,字符串的比較等,都是屬于更為基礎(chǔ)的內(nèi)容,沒有添加到例子當中去,感興趣的同學可以自己找資料去學習。

2.6 map(映射)

map容器和python中的字典非常類似,或者說一模一樣。都是通過鍵值對的方式來存儲和訪問數(shù)據(jù)的,底層是通過紅黑樹來實現(xiàn)的。先來看個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;
}

運行,打印輸出如下結(jié)果:

從以上結(jié)果可以看出,其中數(shù)組直接賦值的方法最簡單直接,最容易理解。當然map保存的是鍵值對,所以前面的int類型數(shù)據(jù)(key)并不代表其位置。比方說,我們將其中的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;
}

當然,同其他的容器類型一樣,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 << "年級" << in_it->first << "號同學:" << in_it->second << endl;
		}
	}
	cout << endl;	
	system("pause");
	return 0;
}

運行,打印輸出如下:

還有一個很重要的問題,就是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詩作: " << 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 << "刪除第一個元素后:" << endl;
	printMap(students);

	map<string, int>::iterator iter = students.find("杜牧");
	students.erase(iter);
	cout << "刪除杜牧后:" << endl;
	printMap(students);
	system("pause");
	return 0;
}

運行后,打印輸出:

從上面的例子也可以看出,map中的鍵值對不一定是按照我們創(chuàng)建的順序保存數(shù)據(jù),map會按照key的值內(nèi)部進行排序,但是保持其鍵值對的對應關(guān)系不變。

2.7 set(集合)

set也是一種關(guān)聯(lián)性容器,它同map一樣,底層使用紅黑樹實現(xiàn),插入刪除操作時僅僅移動指針即可,不涉及內(nèi)存的移動和拷貝,所以效率比較高。從中文名就可以明顯地看出,在set中不會存在重復的元素,若是保存相同的元素,將直接視為無效,我們先來看個簡單的例子(關(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;
}

打印輸出:

上面例子的方法,相當于直接將向量的值賦給了集合,從而順便創(chuàng)建了集合,那么如果想通過逐一賦值的方式創(chuàng)建集合,又該如何編寫代碼呢?如何清除集合中的元素呢?以及是否知道某元素在集合中呢?同樣我們通過一段代碼來看一下。

#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;
}

運行,打印輸出:

通過以上的例子可以發(fā)現(xiàn),set可以直接通過insert()方法添加數(shù)據(jù),而數(shù)據(jù)內(nèi)部是自動排序的,所以不用擔心數(shù)據(jù)的順序問題,當然也可以像map那樣,通過迭代器添加到指定位置,查詢set中有無該數(shù)據(jù)可以直接使用count()方法,有則返回1,無則返回0。

3.后記

到此這篇關(guān)于徹底搞懂C++常見容器的文章就介紹到這了,更多相關(guān)C++常見容器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++中I/O模型之select模型實例

    C++中I/O模型之select模型實例

    這篇文章主要介紹了C++中I/O模型的select模型,實例講述了I/O模型的用法,具有一定的參考借鑒價值,需要的朋友可以參考下
    2014-10-10
  • 詳解C++中的vector容器及用迭代器訪問vector的方法

    詳解C++中的vector容器及用迭代器訪問vector的方法

    使用迭代器iterator可以更方便地解引用和訪問成員,當然也包括vector中的元素,本文就來詳解C++中的vector容器及用迭代器訪問vector的方法,需要的朋友可以參考下
    2016-05-05
  • C語言實現(xiàn)合并字符串

    C語言實現(xiàn)合并字符串

    今天小編就為大家分享一篇C語言實現(xiàn)合并字符串,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • C++任意線程通過hwnd實現(xiàn)將操作發(fā)送到UI線程執(zhí)行

    C++任意線程通過hwnd實現(xiàn)將操作發(fā)送到UI線程執(zhí)行

    做Windows界面開發(fā)時,經(jīng)常需要在多線程環(huán)境中將操作拋到主線程執(zhí)行,下面我們就來學習一下如何在不需要重新定義消息以及接收消息的情況下實現(xiàn)這一要求,感興趣的可以了解下
    2024-03-03
  • C++實現(xiàn)LeetCode(98.驗證二叉搜索樹)

    C++實現(xiàn)LeetCode(98.驗證二叉搜索樹)

    這篇文章主要介紹了C++實現(xiàn)LeetCode(98.驗證二叉搜索樹),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • 解決C語言輸入單個字符屏蔽回車符的問題

    解決C語言輸入單個字符屏蔽回車符的問題

    這篇文章主要介紹了解決C語言輸入單個字符屏蔽回車符的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • C語言實現(xiàn)簡單的推箱子游戲

    C語言實現(xiàn)簡單的推箱子游戲

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)簡單的推箱子游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • C語言深入講解棧與堆和靜態(tài)存儲區(qū)的使用

    C語言深入講解棧與堆和靜態(tài)存儲區(qū)的使用

    對大多數(shù)C 語言初學者來說,堆棧卻是一個很模糊的概念。堆棧是一種數(shù)據(jù)結(jié)構(gòu),一個在程序運行時用于存放的地方,相信這可能是很多初學者共同的認識,靜態(tài)存儲區(qū)即內(nèi)存在程序編譯的時候就已經(jīng)分配好,這塊內(nèi)存在程序的整個運行期間都存在
    2022-04-04
  • C語言?八大排序算法的過程圖解及實現(xiàn)代碼

    C語言?八大排序算法的過程圖解及實現(xiàn)代碼

    排序是數(shù)據(jù)結(jié)構(gòu)中很重要的一章,本文主要為大家介紹了常用的八個排序算法(插入,希爾,選擇,堆排,冒泡,快排,歸并,計數(shù))的過程及代碼實現(xiàn),需要的朋友可以參考一下
    2021-12-12
  • VS2019配置OpenCV時找不到Microsoft.Cpp.x64.user的解決方法

    VS2019配置OpenCV時找不到Microsoft.Cpp.x64.user的解決方法

    這篇文章主要介紹了VS2019配置OpenCV時找不到Microsoft.Cpp.x64.user的解決方法,需要的朋友可以參考下
    2020-02-02

最新評論