const-iterator

what is the difference between const_iterator and iterator? [duplicate]

蹲街弑〆低调 提交于 2019-11-27 17:11:26
This question already has an answer here: What is the difference between const_iterator and non-const iterator in the C++ STL? 7 answers What is difference between these two regarding implementation inside STL. what is the difference regarding performance? I guess when we are traversing the vector in "read only wise", we prefer const_iterator , right? Thank you. ysdx There is no performance difference. A const_iterator is an iterator that points to const value (like a const T* pointer); dereferencing it returns a reference to a constant value ( const T& ) and prevents modification of the

How to change a set element?

时光总嘲笑我的痴心妄想 提交于 2019-11-27 09:31:05
I want to change the element in a set , so I used set<T>::iterator . However, the compiler argues "the element is const". Then I realized that set<T>::iterator is a const_iterator ... So, how I can change the element? Erase it and then insert a new one? The elements of the set will be in sorted order. If you are allowed to modify an element, then this sorting order can not be maintained. Hence you can not modify the item. You need to erase the existing element and insert a new one. EDIT: You cant add an element to a specific location in set. the set should be in the sorted order whatever

Are const_iterators faster?

落花浮王杯 提交于 2019-11-27 03:44:27
Our coding guidelines prefer const_iterator , because they are a little faster compared to a normal iterator . It seems like the compiler optimizes the code when you use const_iterator . Is this really correct? If yes, what really happens internally that makes const_iterator faster?. EDIT: I wrote small test to check const_iterator vs iterator and found varying results: For iterating 10,000 objects const_terator was taking a few milliseconds (around 16 ms) less. But not always . There were iterations in which both were equal. If nothing else, a const_iterator reads better, since it tells

What is the reason behind cbegin/cend?

僤鯓⒐⒋嵵緔 提交于 2019-11-27 02:32:50
I wonder why cbegin and cend were introduced in C++11? What are cases when calling these methods makes a difference from const overloads of begin and end ? Nicol Bolas It's quite simple. Say I have a vector: std::vector<int> vec; I fill it with some data. Then I want to get some iterators to it. Maybe pass them around. Maybe to std::for_each : std::for_each(vec.begin(), vec.end(), SomeFunctor()); In C++03, SomeFunctor was free to be able to modify the parameter it gets. Sure, SomeFunctor could take its parameter by value or by const& , but there's no way to ensure that it does. Not without

How to remove constness of const_iterator?

被刻印的时光 ゝ 提交于 2019-11-26 19:59:55
As an extension to this question Are const_iterators faster? , I have another question on const_iterators . How to remove constness of a const_iterator ? Though iterators are generalised form of pointers but still const_iterator and iterator s are two different things. Hence, I believe, I also cannot use const_cast<> to covert from const_iterator to iterator s. One approach could be that you define an iterator which moves 'til the element to which const_iterator points. But this looks to be a linear time algorithm. Any idea on what is the best way to achieve this? There is a solution with

what is the difference between const_iterator and iterator? [duplicate]

僤鯓⒐⒋嵵緔 提交于 2019-11-26 18:54:02
问题 This question already has answers here : What is the difference between const_iterator and non-const iterator in the C++ STL? (7 answers) Closed last year . What is difference between these two regarding implementation inside STL. what is the difference regarding performance? I guess when we are traversing the vector in "read only wise", we prefer const_iterator , right? Thank you. 回答1: There is no performance difference. A const_iterator is an iterator that points to const value (like a

How to change a set element?

走远了吗. 提交于 2019-11-26 17:50:52
问题 I want to change the element in a set , so I used set<T>::iterator . However, the compiler argues "the element is const". Then I realized that set<T>::iterator is a const_iterator ... So, how I can change the element? Erase it and then insert a new one? 回答1: The elements of the set will be in sorted order. If you are allowed to modify an element, then this sorting order can not be maintained. Hence you can not modify the item. You need to erase the existing element and insert a new one. 回答2:

How to remove constness of const_iterator?

怎甘沉沦 提交于 2019-11-26 17:26:41
问题 As an extension to this question Are const_iterators faster?, I have another question on const_iterators . How to remove constness of a const_iterator ? Though iterators are generalised form of pointers but still const_iterator and iterator s are two different things. Hence, I believe, I also cannot use const_cast<> to covert from const_iterator to iterator s. One approach could be that you define an iterator which moves 'til the element to which const_iterator points. But this looks to be a

Are const_iterators faster?

你说的曾经没有我的故事 提交于 2019-11-26 10:36:20
问题 Our coding guidelines prefer const_iterator , because they are a little faster compared to a normal iterator . It seems like the compiler optimizes the code when you use const_iterator . Is this really correct? If yes, what really happens internally that makes const_iterator faster?. EDIT: I wrote small test to check const_iterator vs iterator and found varying results: For iterating 10,000 objects const_terator was taking a few milliseconds (around 16 ms) less. But not always . There were

What is the reason behind cbegin/cend?

拈花ヽ惹草 提交于 2019-11-26 10:09:19
问题 I wonder why cbegin and cend were introduced in C++11? What are cases when calling these methods makes a difference from const overloads of begin and end ? 回答1: It's quite simple. Say I have a vector: std::vector<int> vec; I fill it with some data. Then I want to get some iterators to it. Maybe pass them around. Maybe to std::for_each : std::for_each(vec.begin(), vec.end(), SomeFunctor()); In C++03, SomeFunctor was free to be able to modify the parameter it gets. Sure, SomeFunctor could take