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

更改 std::map 中元素鍵的最快方法是什么

What is the fastest way to change a key of an element inside std::map(更改 std::map 中元素鍵的最快方法是什么)
本文介紹了更改 std::map 中元素鍵的最快方法是什么的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我理解人們不能這樣做的原因(重新平衡和其他東西):

I understand the reasons why one can't just do this (rebalancing and stuff):

iterator i = m.find(33);

if (i != m.end())
  i->first = 22;

但到目前為止,更改鍵的唯一方法(我知道)是從樹中完全刪除節點,然后使用不同的鍵插入值:

But so far the only way (I know about) to change the key is to remove the node from the tree alltogether and then insert the value back with a different key:

iterator i = m.find(33);

if (i != m.end())
{
  value = i->second;
  m.erase(i);
  m[22] = value;
}

出于更多原因,這對我來說似乎效率很低:

This seems rather inefficient to me for more reasons:

  1. 遍歷樹三次(+ balance)而不是兩次(+ balance)

  1. Traverses the tree three times (+ balance) instead of twice (+ balance)

另一個不必要的值副本

不必要的解除分配然后重新分配樹內的節點

Unnecessary deallocation and then re-allocation of a node inside of the tree

我發現分配和釋放是這三個中最差的.我是不是遺漏了什么,或者有更有效的方法嗎?

I find the allocation and deallocation to be the worst from those three. Am I missing something or is there a more efficient way to do that?

我認為,理論上應該是可能的,所以我認為針對不同的數據結構進行更改是不合理的.這是我想到的偽算法:

I think, in theory, it should be possible, so I don't think changing for a different data structure is justified. Here is the pseudo algorithm I have in mind:

  1. 在樹中找到我想要更改其鍵的節點.

  1. Find the node in the tree whose key I want to change.

如果從樹中分離(不要解除分配)

Detach if from the tree (don't deallocate)

重新平衡

更改分離節點內的密鑰

將節點插入回樹

重新平衡

推薦答案

在 C++17 中,新的 map::extract 函數可讓您更改密鑰.
示例:

In C++17, the new map::extract function lets you change the key.
Example:

std::map<int, std::string> m{ {10, "potato"}, {1, "banana"} };
auto nodeHandler = m.extract(10);
nodeHandler.key() = 2;
m.insert(std::move(nodeHandler)); // { { 1, "banana" }, { 2, "potato" } }

這篇關于更改 std::map 中元素鍵的最快方法是什么的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 轉換單一顏色)
主站蜘蛛池模板: 视频在线一区二区 | 国产日韩精品视频 | 欧美一级电影免费观看 | 亚洲人精品 | 亚洲自拍一区在线观看 | 午夜精品久久久久久久久久久久 | 欧美一级免费黄色片 | 国产区在线| 9久久 | 日本一二三区电影 | 精品99久久久久久 | 青青草原精品99久久精品66 | 男女免费观看在线爽爽爽视频 | 国产精品久久久久久久久图文区 | 国产激情福利 | 国产精品乱码一区二三区小蝌蚪 | 欧美中文一区 | 神马影院一区二区三区 | 精品欧美一区二区三区久久久小说 | 一区二区三区久久久 | 精品国产欧美一区二区三区成人 | 天天爽天天干 | 国产免费一区二区三区最新6 | 中文字幕在线免费观看 | 亚洲97 | 午夜影院黄| 久久精品无码一区二区三区 | 欧美日韩中文在线 | 亚洲欧美一区二区三区国产精品 | 国产乱码精品一区二区三区五月婷 | 蜜桃久久 | 久久精品99久久 | 精品av天堂毛片久久久借种 | 亚洲综合精品 | 久www | 一区二区三区免费 | 日本视频免费观看 | 欧美二区在线 | 国产精品久久久久久久久久妇女 | 91中文字幕在线观看 | 中文一区|