简体   繁体   English

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

[英]Change/update value using std::map const_iterator

I am just curious to know if I can change/update the map's value using const_iterator . 我只是想知道我是否可以使用const_iterator更改/更新地图的值。

Below is the code snippet: 下面是代码片段:

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;
}

Thank you in advance for sharing your ideas. 预先感谢您分享您的想法。

The whole point of const_iterator is that it cannot be used to modify the container. const_iterator的全部要点是它不能用于修改容器。 So no. 所以不行。

Can I change/update the map's value using const_iterator? 我可以使用const_iterator更改/更新地图的值吗?

No ! 不行

It's called const iterator for a reason. 由于某种原因,它称为const迭代器。 As mentioned here , A const_iterator is an iterator that points to const value (like a const T* pointer); 如所提到的在这里 ,A const_iterator是指向常量的值(如一个迭代器const T*指针); dereferencing it returns a reference to a constant value ( const T& ) and prevents modification of the referenced value: it enforces const -correctness . 取消引用将返回对常量值( const T& )的引用,并防止修改引用的值:它强制执行const -correctness

The compiler should not allow modifying the contents of a map through a const_iterator . 编译器不应允许通过const_iterator修改map的内容。 The line 线

itr->second = 30;

should be reported as an error. 应该报告为错误。 If your compiler allows that line, then it is not standards compliant. 如果您的编译器允许该行,则说明它不符合标准。 Perhaphs it allows that line to be compiled through flags that allow standards-incompatible behavior. Perhaphs允许通过允许标准不兼容行为的标志来编译该行。

Using g++, I get the following error. 使用g ++,出现以下错误。

socc.cc: In function ‘int main()’:
socc.cc:12:19: error: assignment of member ‘std::pair<const int, int>::second’ in read-only object
     itr->second = 30;

const迭代器被认为可以防止用户通过任何方式更改/修改值。

It is not possible to change an element through a const iterator. 无法通过构造器更改元素。 That's the most important distinction between a const iterator and non-const iterator. 这是const迭代器与非const迭代器之间最重要的区别。

std::map::begin returns a non-const iterator (assuming the object operand is non-const), so there is no need to use a const iterator in the first place. std::map::begin返回一个非常量迭代器(假设对象操作数为非常量),因此无需首先使用常量迭代器。

However, if for some reason (not demonstrated in the example) you can only have a const iterator, but have non-const access to the container, then you can get a non-const iterator to the element pointed by the const iterator. 但是,如果由于某种原因(在示例中未演示),您只能具有const迭代器,但可以对容器进行非const访问,则可以将非const迭代器获取到const迭代器指向的元素。 This can be achieved by using following, which looks like a trick: 这可以通过使用以下技巧来实现,该技巧看起来像个把戏:

map <int, int>::iterator mutable_itr = m.erase(itr, itr);

It doesn't erase anything, because [itr, itr) is an empty range. 它不会删除任何内容,因为[itr, itr)是一个空范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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