How to update std::map after using the find method?

后端 未结 6 1363
自闭症患者
自闭症患者 2020-12-12 12:11

How to update the value of a key in std::map after using the find method?

I have a map and iterator declaration like this:

         


        
6条回答
  •  甜味超标
    2020-12-12 13:04

    std::map::find returns an iterator to the found element (or to the end() if the element was not found). So long as the map is not const, you can modify the element pointed to by the iterator:

    std::map m;
    m.insert(std::make_pair('c', 0));  // c is for cookie
    
    std::map::iterator it = m.find('c'); 
    if (it != m.end())
        it->second = 42;
    

提交回复
热议问题