C map應用之insert方法

2021-06-16 17:27:19 字數 604 閱讀 7158

直接上**,兩種方式:

mapm_map;

m_map.insert(map::value_type("hello",5));

m_map.insert(make_pair("hello",5));

也就是說,insert後面的資料是pair型別或者是value_type型別了,然而對c++有了解的人都明白,其實value_type和pair是等價的、insert()中的引數必須是value_type型別,那麼為什麼insert()中的引數能夠使用make_pair產生的pair呢?

其實,因為我們在加入pair時的k已經是常量了,所以可以加入。。。而正常來講這都是所有編譯器所能接受的。

在insert插入的同時,還有返回值來說明是否插入成功,就是pair::iterator,bool>>型別,如本例項pair::iterator,bool>> rent= m_map.insert(make_pair("hello",5));

rent->second即是成功與否的標誌;rent->first就是返回的map::iterator迭代器;rent->first.first就是string型別的資料。

這些看起來都非常的麻煩,但是只要信心的研究、編譯、除錯就可以了。

C map應用之insert方法

直接上 兩種方式 mapm map m map.insert map value type hello 5 m map.insert make pair hello 5 也就是說,insert後面的資料是pair型別或者是value type型別了,然而對c 有了解的人都明白,其實value typ...

C map巢狀應用

最近開發中要用到stl,然後自己檢視了一些資料,並寫了一些 在使用中,想起了如果是map巢狀,該如何應用呢?下面是我的coding內容 對於傳統的map,我們只需要 include include int main return 0 結果輸出 100 maxi 200 xiaoma 300 xiao...

c map函式的應用

資料的插入 第一種 用insert函式插入pair資料 mapmapstudent mapstudent.insert pair 0,student one mapstudent.insert pair 1,student two mapstudent.insert pair 2,student t...