a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C > 正文

vector, list, map在遍歷時刪除符合條件的元素實現方法

2020-01-26 14:20:16
字體:
來源:轉載
供稿:網友

直接看源碼,內有詳細解釋

/*	測試vector, list, map遍歷時刪除符合條件的元素	本代碼測試環境: ubuntu12 + win7_x64*/#include <iostream>#include <vector>#include <list>#include <map>#include <iterator>using namespace std;void Remove1(vector<int> &vec, int num){	for (vector<int>::iterator it = vec.begin(); it != vec.end();)	{		if (*it == num)			it = vec.erase(it);		else			it++;	}}void Remove2(list<int> &lst, int num){	list<int>::iterator it;	for (it=lst.begin(); it!=lst.end();)	{		if (*it == num)		{			lst.erase(it++);		}		else			it++;	}}void initMap(map<int, int>& m, int arr[], int arrLen){	for(int i = 0; i < arrLen; i++)		m[i] = arr[i];}void Remove_map(map<int, int>& m, int num){	map<int, int>::iterator it;	for(it = m.begin(); it != m.end();)	{		if (it->second == num)			m.erase(it++);		else			it++;	}}void displayMap(map<int, int>& m){	map<int, int>::iterator it = m.begin();	while(it != m.end())	{		cout << "key = " << it->first << ", value = " << it->second << endl;		it++;	}	cout << endl;}int main(void){	int arr[] = {1, 3, 5, 5, 5, 13, 7, 5, 7, 9};	int arrLen = sizeof(arr) / sizeof(arr[0]);#if 1	// test vector	vector<int> vec(arr, arr+arrLen);	Remove1(vec, 5);	copy(vec.begin(), vec.end(), ostream_iterator<int>(cout, " "));	cout << endl << endl;#endif	// test list	list<int> lst(arr, arr+arrLen);	Remove2(lst, 5);	copy(lst.begin(), lst.end(), ostream_iterator<int>(cout, " "));	cout << endl << endl;	// test map	map<int, int> m;	initMap(m, arr, arrLen);	Remove_map(m, 5);	displayMap(m);	return 0;}/*Win7_x64運行結果:1 3 13 7 7 91 3 13 7 7 9key = 0, value = 1key = 1, value = 3key = 5, value = 13key = 6, value = 7key = 8, value = 7key = 9, value = 9Ubuntu12運行結果:[zcm@cpp #54]$makeg++ -Wall -Os -DLINUX -o a a.cpp[zcm@cpp #55]$./a1 3 13 7 7 9 1 3 13 7 7 9 key = 0, value = 1key = 1, value = 3key = 5, value = 13key = 6, value = 7key = 8, value = 7key = 9, value = 9*/

以上就是小編為大家帶來的小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧全部內容了,希望大家多多支持武林網~

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 精品无码久久久久国产 | 色视频久久 | 久久久久综合网 | 欧美日韩一区二区三区在线观看 | 日本福利在线 | 日本va欧美va精品发布 | 久久一二 | 午夜精品亚洲日日做天天做 | 日本免费在线观看 | 欧美日韩精品一二区 | 四虎免费在线播放 | 中文字幕一区二区三区精彩视频 | 成人精品一区 | 国产a网站| 视频一区二区三区在线观看 | 欧美午夜视频在线观看 | 精品一区二区三区久久 | 精品国产乱码久久久久久88av | 亚洲成人中文字幕 | 欧美高清视频一区二区三区 | 国产精品久久久久9999鸭 | 一区免费 | 国产精品一任线免费观看 | 国产欧美一区二区 | 国产精品一区久久久久 | 青青草在线视频免费观看 | 黄色性视频 | www.av在线 | 中文字幕亚洲欧美 | 99精品视频免费在线观看 | 亚洲一级免费视频 | 国内精品一区二区 | 亚洲精品三级 | 成人亚洲| 日韩欧美大片在线观看 | 成人在线播放网站 | 探花网站| 欧美激情视频久久 | 91 在线观看 | 理论片一区 | 欧美成人一二区 |