簡體   English   中英

在地圖和constness中插入一對?

[英]Inserting a pair into the map and constness?

在任何以下示例中,有什么正當理由為什么一個在一對中要比第三對好?

map<const int, int> test;

test.insert(const pair<const int, int>(3, 9));
test.insert(const pair<int, int>(3, 9));
test.insert(pair<int, int>(3, 9));
test.insert(pair<const int, int>(5, 9));

基本上是。 愚蠢的編譯器將在此代碼中調用一個額外的轉換構造函數

test.insert(pair<const int, int>(5, 9));

但是優化的編譯器將使其與其他替代產品相同。

我只問,誰願意寫這樣的代碼? 通常,我會寫test.insert(std::make_pair(3, 9))並不在乎。

插入地圖時,最后2對沒有區別。 map中的鍵默認為const,只需使用make_pair函數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM