我有一個非常性能密集的應用程序,我真的想避免製作一個保存在unordered_map中的字符串的副本。我希望能夠做的一個例子是將該字符串與本地字符串變量進行比較。是否可以訪問unordered_map中的元素而不復制它的副本?
例如,
unordered_map<string,string> X;
string test = "def";
X["abc"] = test;
//other operations here...
string* map_entry = X.???; //some operation that doesn't make a copy of the string
size_t map_entry_size = (*map_entry).size();
for (size_t i = 0; i < map_entry_size; ++i)
{
if ((*map_entry)[i] != test[i])
throw 1;
}
這是可能的,或者必須我一直在使用它之前使元素的副本?
不清楚爲什麼你認爲你需要複製。 'operator []'返回一個引用,而不是一個副本... – user463035818
取消引用迭代器返回一個引用,所以你沒有複製那裏。 – Jarod42
無關:如果你曾經需要避免複製使用參考而不是指向安全的指針,你一些不必要的頭痛 – user463035818