map中插入資料

2021-10-23 03:49:36 字數 994 閱讀 7343

**:

在構造map容器後,我們就可以往裡面插入資料了。這裡講四種插入資料的方法:

第一種:用insert函式插入pair資料:在vc下**入這條語句,遮蔽4786警告 #pragma warning (disable:4786) )

mapmapstudent;  

mapstudent.insert(pair(1, "student_one"));  

mapstudent.insert(pair(2, "student_two"));  

mapstudent.insert(pair(3, "student_three")); 

第二種:用insert函式插入value_type資料,下面舉例說明

mapmapstudent;  

mapstudent.insert(map::value_type (1, "student_one"));  

mapstudent.insert(map::value_type (2, "student_two"));  

mapstudent.insert(map::value_type (3, "student_three"));

第三種:在insert函式中使用make_pair()函式,下面舉例說明

mapmapstudent;  

mapstudent.insert(make_pair(1, "student_one"));  

mapstudent.insert(make_pair(2, "student_two"));  

mapstudent.insert(make_pair(3, "student_three"));  

第四種:用陣列方式插入資料,下面舉例說明

mapmapstudent;  

mapstudent[1] = "student_one";  

mapstudent[2] = "student_two";  

mapstudent[3] = "student_three";  

STL中map容器的元素插入

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

map插入物件小結

難道插入map還有什麼講究嗎?我們且看map在stl中的定義方法 template class alloc alloc 第乙個引數key是關鍵字型別 第二個引數t是值型別 第三個引數compare是比較函式 仿函式 第四個引數是記憶體配置物件 map內部儲存機制實際是以紅黑樹為基礎,紅黑樹在插入節點...

map插入物件小結

難道插入map還有什麼講究嗎?我們且看map在stl中的定義方法 template class alloc alloc 第乙個引數key是關鍵字型別 第二個引數t是值型別 第三個引數compare是比較函式 仿函式 第四個引數是記憶體配置物件 map內部儲存機制實際是以紅黑樹為基礎,紅黑樹在插入節點...