C 中的map容器的刪除 插入 遍歷

2021-10-13 23:09:42 字數 1088 閱讀 6476

#include

#include

#include

#include

using

namespace std;

struct display};

intmain()

else

//使用迭代器完成遍歷

for(iter = scores.

begin()

; iter!=scores.

end(

);iter++

)//把scores值小於90的鍵值對刪除

for(iter = scores.

begin()

; iter!=scores.

end(

);iter++)}

for_each

(scores.

begin()

, scores.

end(),

display()

);cout <<

"-------"

<< endl;

//刪除元素的另一種方法,通過find查詢,再刪除

iter = scores.

find

("xiaoming");

scores.

erase

(iter)

;for_each

(scores.

begin()

, scores.

end(),

display()

);cout <<

"-------"

<< endl;

//另一種刪除元素的方法,若返回不為0,表示刪除了,若返回為0,表示沒有找到

int n = scores.

erase

("lisi");

cout << n << endl;

//清空map容器

scores.

erase

(scores.

begin()

, scores.

end())

;return0;

}

map容器的插入和刪除

插入的四種方式 會按照key進行排序 map int,int m1 插入方式 1.m1.insert pair int,int 2 520 2.m1.insert make pair 1 2333 3.m1.insert map int,int value type 0 12345 4.m1 3 5...

STL中map容器的元素插入

stl中的map容器是我經常用的,但是因為map跟別的容器不太一樣,每次用的時候對於map中元素的插入方式總是忘卻,故而發篇博文,提醒我也提醒所有人map容器的三種插入方式 第一種 用insert函式插入pair資料。下面舉例說明 include include include using name...

C 中map的遍歷

一 點睛 map資料的遍歷,也有3種方法 二 map反向迭代器的使用實戰 1 include include include using namespace std int main mapmapstudent mapstudent 1 student one mapstudent 2 studen...