vector, list, map在遍歷時刪除符合條件的元素實現(xiàn)方法
直接看源碼,內(nèi)有詳細解釋
/*
測試vector, list, map遍歷時刪除符合條件的元素
本代碼測試環(huán)境: 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 9
1 3 13 7 7 9
key = 0, value = 1
key = 1, value = 3
key = 5, value = 13
key = 6, value = 7
key = 8, value = 7
key = 9, value = 9
Ubuntu12運行結果:
[zcm@cpp #54]$make
g++ -Wall -Os -DLINUX -o a a.cpp
[zcm@cpp #55]$./a
1 3 13 7 7 9
1 3 13 7 7 9
key = 0, value = 1
key = 1, value = 3
key = 5, value = 13
key = 6, value = 7
key = 8, value = 7
key = 9, value = 9
*/
以上就是小編為大家?guī)淼男【幱X得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧全部內(nèi)容了,希望大家多多支持我們~
上一篇:c語言實現(xiàn)把文件中數(shù)據(jù)讀取并存到數(shù)組中
欄 目:C語言
下一篇:解決在Mac下直接解壓C++靜態(tài)庫出現(xiàn)的問題
本文標題:vector, list, map在遍歷時刪除符合條件的元素實現(xiàn)方法
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1896.html
您可能感興趣的文章
- 01-10異步http listener 完全并發(fā)處理懲罰http懇求的小例子
- 01-10基于C++ list中erase與remove函數(shù)的使用詳解
- 01-10使用map實現(xiàn)單詞轉換的實例分析
- 01-10海量數(shù)據(jù)處理系列之:用C++實現(xiàn)Bitmap算法
- 01-10求素數(shù),用vector存儲的實現(xiàn)方法
- 01-10解析bitmap處理海量數(shù)據(jù)及其實現(xiàn)方法分析
- 01-10va_list(),va_start(),va_arg(),va_end() 詳細解析
- 01-10map插入自定義對象總結
- 01-10STL list鏈表的用法詳細解析
- 01-10關于STL中vector容器的一些總結


閱讀排行
本欄相關
- 04-02c語言函數(shù)調用后清空內(nèi)存 c語言調用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調用函數(shù)求fibo C語言調用函數(shù)求
隨機閱讀
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實例總結
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設置


