久久久久久久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视频一区 | 欧美成人一区二免费视频软件 | 国产美女永久免费无遮挡 | 青久草视频 | 亚洲 欧美 日韩在线 | 日本久久久久久 | 日韩在线中文字幕 | 免费h视频 | 欧美在线一区二区三区 | 国产视频精品在线观看 | 日韩精品一二三区 | 色欧美综合 | 亚洲欧美在线观看视频 | 精品一级 | 综合一区二区三区 | 一二三区在线 | 国产欧美一区二区在线观看 | 国产a级毛片 | 国产在线精品一区二区三区 | 九九热精品视频在线观看 | 国产精品一区二区视频 | av在线播放一区二区 | 黄视频网站免费观看 | 亚洲欧美日韩激情 | 亚洲天堂二区 | 97精品国产97久久久久久免费 | 久久综合一区 | 人人干人人干人人干 | 这里只有精品99re | 视频在线观看一区 | 91精品午夜窝窝看片 | 亚洲一区二区不卡在线观看 | 久久久夜 | 中文字幕第一页在线 | 欧美一区二区在线 | 久久久免费观看视频 | 欧美一级一 |