久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 STL 映射中,使用 map::insert 是否比使用 [] 更好

In STL maps, is it better to use map::insert than []?(在 STL 映射中,使用 map::insert 是否比使用 [] 更好?)
本文介紹了在 STL 映射中,使用 map::insert 是否比使用 [] 更好?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

前段時間和同事討論如何在STL中插入值地圖.我更喜歡 map[key] = value; 因為它感覺自然并且清晰易讀,而他更喜歡 map.insert(std::make_pair(key, value)).

A while ago, I had a discussion with a colleague about how to insert values in STL maps. I preferred map[key] = value; because it feels natural and is clear to read whereas he preferred map.insert(std::make_pair(key, value)).

我剛剛問過他,我們都不記得為什么 insert 更好的原因,但我確信這不僅僅是一種風格偏好,而是有一個技術原因,比如效率.SGI STL 參考 簡單地說:嚴格來說,這個成員函數是不必要:它的存在只是為了方便."

I just asked him and neither of us can remember the reason why insert is better, but I am sure it was not just a style preference rather there was a technical reason such as efficiency. The SGI STL reference simply says: "Strictly speaking, this member function is unnecessary: it exists only for convenience."

誰能告訴我這個原因,還是我只是在夢想有一個?

Can anybody tell me that reason, or am I just dreaming that there is one?

推薦答案

當你寫作時

map[key] = value;

無法判斷您是否替換keyvalue,或者您是否創建了一個新的keyvalue.

there's no way to tell if you replaced the value for key, or if you created a new key with value.

map::insert() 只會創建:

using std::cout; using std::endl;
typedef std::map<int, std::string> MyMap;
MyMap map;
// ...
std::pair<MyMap::iterator, bool> res = map.insert(MyMap::value_type(key,value));
if ( ! res.second ) {
    cout << "key " <<  key << " already exists "
         << " with value " << (res.first)->second << endl;
} else {
    cout << "created key " << key << " with value " << value << endl;
}

對于我的大部分應用,我通常不在乎我是創建還是替換,所以我使用更容易閱讀的map[key] = value.

For most of my apps, I usually don't care if I'm creating or replacing, so I use the easier to read map[key] = value.

這篇關于在 STL 映射中,使用 map::insert 是否比使用 [] 更好?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Assertion failed (size.widthgt;0 amp;amp; size.heightgt;0)(斷言失敗(size.width0 amp;amp; size.height0))
Rotate an image in C++ without using OpenCV functions(在 C++ 中旋轉圖像而不使用 OpenCV 函數)
OpenCV: process every frame(OpenCV:處理每一幀)
Why can#39;t I open avi video in openCV?(為什么我不能在 openCV 中打開 avi 視頻?)
OpenCV unable to set up SVM Parameters(OpenCV 無法設置 SVM 參數)
Convert a single color with cvtColor(使用 cvtColor 轉換單一顏色)
主站蜘蛛池模板: 性视频一区 | 精品久久国产 | 色婷婷亚洲国产女人的天堂 | 国产精品视频网 | 日韩一级黄色片 | 久久久婷| 久久高清 | 中文字幕免费观看 | 亚洲精品久久久久久国产精华液 | 最新中文字幕第一页视频 | 日韩欧美在线精品 | 久久免费视频网 | 在线成人免费视频 | 日韩精品区 | 91精品久久久久久久久中文字幕 | 国产精品av久久久久久毛片 | 香蕉久久av| 国产精品亚洲成在人线 | 在线观看特色大片免费网站 | 日韩欧美国产一区二区 | 日本又色又爽又黄又高潮 | 国产精品亚洲精品 | 人人干人人干人人干 | 蜜臀网 | 一区二区三区欧美大片 | 日韩在线视频免费观看 | 欧美日韩高清一区 | 亚洲精品91| 久久久久电影 | 一级在线观看 | 久久精品二区亚洲w码 | 国产精品久久久久久久久久久久久 | 久久i| 国产精品一区二区在线播放 | 国产精品久久久久久一区二区三区 | 精品久久久久久久久久久 | 一区二区三区四区不卡视频 | 91小视频在线 | 日本国产精品视频 | 国产成人精品999在线观看 | h在线免费观看 |