gru*_*czy 0 c++ dictionary unique-ptr
我有一个map<std::string, std::unique_ptr<Cls>> my_map
. 我想从这张地图中移出一些价值,以获得以下内容:
std::unique_ptr<Cls> cls = my_map.get_and_erase("some str");
Run Code Online (Sandbox Code Playgroud)
erase
不幸的是不返回值。我在这里最好的方法是什么?
从 C++17 开始,你有std::map::extract
:
// if the key exists in the map, it'll be deleted after this:
auto cls_node = my_map.extract("some str");
if(not cls_node.empty()) // cls_node.value() gives access to the value
Run Code Online (Sandbox Code Playgroud)