使用std :: map const_iterator更改/更新值

Pan*_*apa 0 c++ stl stdmap const-iterator

我只是想知道我是否可以使用const_iterator更改/更新地图的值。

下面是代码片段:

int main()
{
    map <int, int> m;
    m.insert(make_pair(1, 10));

    map <int, int>::const_iterator itr = m.begin(); //The iterator is const_iterator
    itr->second = 30;

    cout << itr->second; //The value to be printed is 30, and not 10.
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

预先感谢您分享您的想法。

Jes*_*uhl 7

const_iterator是,它不能被用来修改容器。所以不行。