bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

C++常見容器一網打盡-創新互聯

1.概述

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

讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業的熱愛。我們立志把好的技術通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領域值得信任、有價值的長期合作伙伴,公司提供的服務項目有:域名申請、網絡空間、營銷軟件、網站建設、新城網站維護、網站推廣。2.容器詳解 2.1vector(向量)

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

#include#includeusing namespace std;
// 程序的主函數
int main()
{vectorV;
	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
從上面的例子可以看出,向量和數組的用法極其類似。當然,容器還有一個極其好用的功能,就是容器的嵌套使用。

#include#includeusing namespace std;
// 程序的主函數
int main()
{vector>V;
	vectorsub_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這個時候的向量可以看作是一個二維數組,當然比二維數組更加靈活、強大。
當然向量容器還有其他更加豐富的操作。比如:

int size = vec1.size();         //元素個數
 
    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::iterator iter = vec1.begin();    //獲取迭代器首地址
 
    vector::const_iterator c_iter = vec1.begin();   //獲取const類型迭代器
 
    vec1.clear();                 //清空元素

舉個最常見的例子:

#include#includeusing namespace std;
// 程序的主函數
int main()
{vectorV;
	V.push_back(1);
	V.push_back(2);
	V.push_back(3);
	for (vector::iterator it = V.begin(); it != V.end(); it++)
		cout<< *it<< " ";
	cout<< endl;
	cout<< "=========================="<< endl;
	V.insert(V.begin() + 2,10);
	for (vector::iterator it = V.begin(); it != V.end(); it++)
		cout<< *it<< " ";
	system("pause");
	return 0;
}

注意如果不是在其尾部插入數據,要傳入插入位置的迭代器。
打印輸出:
在這里插入圖片描述

2.2deque(雙端隊列)

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

#include#includeusing namespace std;
// 程序的主函數
int main()
{dequeD;
	D.push_back(1);
	D.push_back(2);
	D.push_back(3);
	for (deque::iterator it = D.begin(); it != D.end(); it++)
		cout<< *it<< " ";
	cout<< endl;
	cout<< "============在其索引2的位置插入10:"<< endl;
	D.insert(D.begin() + 2,10);
	for (deque::iterator it = D.begin(); it != D.end(); it++)
		cout<< *it<< " ";
	cout<< endl;
	cout<< "============在其頭部插入0:"<< endl;
	D.push_front(0);
	for (deque::iterator it = D.begin(); it != D.end(); it++)
		cout<< *it<< " ";
	cout<< endl;
	cout<< "============在其頭部彈出0:"<< endl;
	D.pop_front();
	for (deque::iterator it = D.begin(); it != D.end(); it++)
		cout<< *it<< " ";
	system("pause");
	return 0;
}

打印輸出:
在這里插入圖片描述

2.3list(列表)

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

#include#includeusing namespace std;
// 程序的主函數
int main()
{//list的創建和初始化
	listlst1;          //創建空list


	listlst2(3);       //創建含有三個元素的list


	listlst3(3, 2); //創建含有三個元素的值為2的list


	listlst4(lst3);    //使用lst3初始化lst4


	listlst5(lst3.begin(), lst3.end());  //同lst4
	cout<< "lst4中的元素有:"<< endl;
	for (list::iterator it = lst4.begin(); it != lst4.end(); it++)
		cout<< *it<< " ";
	cout<< endl;
	cout<< "lst5中的元素有:"<< endl;
	for (list::iterator it = lst5.begin(); it != lst5.end(); it++)
		cout<< *it<< " ";
	cout<< endl;
	system("pause");
	return 0;
}

運行,打印輸出:
在這里插入圖片描述
然后再來看一個元素的添加,排序的例子。

#include#include#includeusing namespace std;
// 程序的主函數
int main()
{//list的創建和初始化
	listlst1;          //創建空list

	for(int i = 0; i< 10; i++)
		lst1.push_back(9-i);                    //添加值
	cout<< "lst1中的元素有:"<< endl;
	for (list::iterator it = lst1.begin(); it != lst1.end(); it++)
		cout<< *it<< " ";
	cout<< endl;
	cout<< "對lst1中的元素進行排序:"<< endl;
	lst1.sort();
	for (list::iterator it = lst1.begin(); it != lst1.end(); it++)
		cout<< *it<< " ";
	cout<< endl;
	cout<< "在索引為5的地方插入999:"<< endl;
	list::iterator insert_it = lst1.begin();
	for (int i = 0; i< 5; i++)
		insert_it++;
	lst1.insert(insert_it, 3, 999);
	for (list::iterator it = lst1.begin(); it != lst1.end(); it++)
		cout<< *it<< " ";
	cout<< endl;
	cout<< "刪除相鄰重復元素后:"<< endl;
	lst1.unique();                         //刪除相鄰重復元素
	for (list::iterator it = lst1.begin(); it != lst1.end(); it++)
		cout<< *it<< " ";
	cout<< endl;
	system("pause");
	return 0;
}

運行后,打印輸出:
在這里插入圖片描述
特別注意,由于list的底層是雙向鏈表,因此insert操作無法直接像向量雙端隊列一樣直接插入數據,只能通過迭代器的自加移動到相應位置,再插入數據。

2.4 array(數組)

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

#include#include#include 
using namespace std;
// 程序的主函數
int main()
{arrayarr = {1, 3, 2};
	cout<< "arr values:"<< std::endl;
	for (array::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也支持嵌套,可以采用這樣的方式來構建二維(多維)數組,由于比較簡單,就不舉例了。

2.5 string(字符串)

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

#include#includeusing namespace std;
// 程序的主函數
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;
}

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

2.6 map(映射)

map容器和python中的字典非常類似,或者說一模一樣。都是通過鍵值對的方式來存儲和訪問數據的,底層是通過紅黑樹來實現的。先來看個map的創建以及初始化的例子。

#include#include#includeusing namespace std;
// 程序的主函數
int main()
{//map的創建和初始化
	//第一種:用insert函數插入pair數據:
	mapmy_map;
	my_map.insert(pair(1, "first"));
	my_map.insert(pair(2, "second"));
	//第二種:用insert函數插入value_type數據:
	my_map.insert(map::value_type(3, "first"));
	my_map.insert(map::value_type(4, "second"));
	//第三種:用數組的方式直接賦值:
	my_map[5] = "first";
	my_map[6] = "second";
	map::iterator it;           //迭代器遍歷
	for (it = my_map.begin(); it != my_map.end(); it++)
		cout<< it->first<< "->"<second<< endl;
	system("pause");
	return 0;
}

運行,打印輸出如下結果:
在這里插入圖片描述
從以上結果可以看出,其中數組直接賦值的方法最簡單直接,最容易理解。當然map保存的是鍵值對,所以前面的int類型數據(key)并不代表其位置。比方說,我們將其中的int修改為float也是可以的。代碼如下:

#include#include#includeusing namespace std;
// 程序的主函數
int main()
{//map的創建和初始化
	//第一種:用insert函數插入pair數據:
	mapmy_map;
	my_map.insert(pair(1, "first"));
	my_map.insert(pair(2, "second"));
	//第二種:用insert函數插入value_type數據:
	my_map.insert(map::value_type(3, "first"));
	my_map.insert(map::value_type(4, "second"));
	//第三種:用數組的方式直接賦值:
	my_map[5.3] = "first";
	my_map[6.6] = "second";
	map::iterator it;           //迭代器遍歷
	for (it = my_map.begin(); it != my_map.end(); it++)
		cout<< it->first<< "->"<second<< endl;
	system("pause");
	return 0;
}

當然,同其他的容器類型一樣,map同樣支持嵌套,比如:

#include#include#includeusing namespace std;
// 程序的主函數
int main()
{//map的嵌套用法
	map>my_map;
	my_map[1][1] = "張三";
	my_map[1][2] = "李四";
	my_map[1][3] = "王五";

	for (map>::iterator it = my_map.begin(); it != my_map.end(); it++)
	{for (map::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#include#includeusing namespace std;

void printMap(const map& 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[]) {mapstudents;
	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::iterator iter = students.find("杜牧");
	students.erase(iter);
	cout<< "刪除杜牧后:"<< endl;
	printMap(students);
	system("pause");
	return 0;
}

運行后,打印輸出:
在這里插入圖片描述
從上面的例子也可以看出,map中的鍵值對不一定是按照我們創建的順序保存數據,map會按照key的值內部進行排序,但是保持其鍵值對的對應關系不變。

2.7 set(集合)

set也是一種關聯性容器,它同map一樣,底層使用紅黑樹實現,插入刪除操作時僅僅移動指針即可,不涉及內存的移動和拷貝,所以效率比較高。從中文名就可以明顯地看出,在set中不會存在重復的元素,若是保存相同的元素,將直接視為無效,我們先來看個簡單的例子(關于set的創建和元素的添加等):

#include#include#includeusing namespace std;
// 程序的主函數
int main()
{vectorivec;
	for (vector::size_type i = 0; i != 10; i++) {ivec.push_back(i);
		ivec.push_back(i);
	}
	setiset(ivec.begin(), ivec.end());
	cout<< "向量中的元素為:"<< endl;
	for (vector::iterator it = ivec.begin(); it != ivec.end(); it++)
	{cout<< *it<< " ";
	}
	cout<< endl;
	cout<< "集合中的元素為:"<< endl;
	for (set::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;
}

打印輸出:
在這里插入圖片描述
上面例子的方法,相當于直接將向量的值賦給了集合,從而順便創建了集合,那么如果想通過逐一賦值的方式創建集合,又該如何編寫代碼呢?如何清除集合中的元素呢?以及是否知道某元素在集合中呢?同樣我們通過一段代碼來看一下。

#include#include#include#includeusing namespace std;
// 程序的主函數
int main()
{setset1;
	set1.insert("the"); 

	//刪除集合
	while (!set1.empty())
	{//獲取頭部
		set::iterator it = set1.begin();
		//打印頭部元素
		cout<< *it<< endl;

		//從頭部刪除元素
		set1.erase(set1.begin());
	}
	setset2;
	for (int i = 100; i< 110; i++)
		set2.insert(i);
	cout<< "set2中5出現的次數為:";
	cout<< set2.count(5)<< endl;
	set2.clear();
	cout<< "set2清除之后的大小為:";
	cout<< set2.size()<< endl;
	system("pause");
	return 0;
}

運行,打印輸出:
在這里插入圖片描述
通過以上的例子可以發現,set可以直接通過insert()方法添加數據,而數據內部是自動排序的,所以不用擔心數據的順序問題,當然也可以像map那樣,通過迭代器添加到指定位置,查詢set中有無該數據可以直接使用count()方法,有則返回1,無則返回0。

3.后記

這篇帖子是我在今年(2022)的國慶期間完成的,誠然放棄了很多游玩的時間,和親人團聚的時間,但是我一直相信努力的意義和重要性,很多時候我們不知道什么時候自己會成功,也不知道自己是否會成功,但我們仍需趁著年輕,努力一把,拼搏一把。在小說《牧羊少年奇幻之旅》中有這樣一句話:“當你想要某種東西時,整個宇宙會合力祝你實現愿望。”加油?。。?img src="/upload/otherpic22/9d800661d4a546b788ca3a3ab36a1966.jpg" alt="請添加圖片描述" />

你是否還在尋找穩定的海外服務器提供商?創新互聯www.cdcxhl.cn海外機房具備T級流量清洗系統配攻擊溯源,準確流量調度確保服務器高可用性,企業級服務器適合批量采購,新人活動首月15元起,快前往官網查看詳情吧

網頁題目:C++常見容器一網打盡-創新互聯
轉載來源:http://vcdvsql.cn/article42/cseoec.html

成都網站建設公司_創新互聯,為您提供微信公眾號移動網站建設品牌網站建設外貿建站網頁設計公司、動態網站

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

外貿網站建設