简体   繁体   中英

Is it safe to reassign std::map::value_type&?

If I have a map with a set of exist key-value pairs, is it safe for me to reassign direct to an iterator? For example:

bool switchIf(const std::string& key, Widget& widget)
{
    WidgetMap::iterator iter = _widgets.find(key);
    if(_widgets.end() != iter)
    { 
        iter->second = widget;
    }
    return (_widgets.end() != iter);
}

Yes. That is completely safe. iter->second is nothing, but a reference to the value in the map.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM