Questions tagged [const-iterator]

A C++ STL iterator with strictly read-only access.

const_iterator is an with strictly read-only access.

161 questions
361
votes
8 answers

How to implement an STL-style iterator and avoid common pitfalls?

I made a collection for which I want to provide an STL-style, random-access iterator. I was searching around for an example implementation of an iterator but I didn't find any. I know about the need for const overloads of [] and * operators. What…
Tamás Szelei
  • 23,169
  • 18
  • 105
  • 180
303
votes
9 answers

How to correctly implement custom iterators and const_iterators?

I have a custom container class for which I'd like to write the iterator and const_iterator classes. I never did this before and I failed to find an appropriate how-to. What are the guidelines regarding iterator creation, and what should I be aware…
ereOn
  • 53,676
  • 39
  • 161
  • 238
219
votes
7 answers

What is the reason behind cbegin/cend?

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?
Andriy
  • 8,486
  • 3
  • 27
  • 51
153
votes
3 answers

what is the difference between const_iterator and iterator?

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.
user658266
  • 2,397
  • 4
  • 20
  • 14
57
votes
10 answers

How to remove constness of const_iterator?

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 iterators are two…
aJ.
  • 34,624
  • 22
  • 86
  • 128
39
votes
11 answers

Are const_iterators faster?

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…
aJ.
  • 34,624
  • 22
  • 86
  • 128
36
votes
3 answers

Is comparison of const_iterator with iterator well-defined?

Consider the following code: #include #include int main() { std::vector vec{1,2,3,5}; for(auto it=vec.cbegin();it!=vec.cend();++it) { std::cout << *it; // A typo: end instead of cend …
Ruslan
  • 18,162
  • 8
  • 67
  • 136
29
votes
1 answer

New std::map::erase() signature C++17

According to this answer, an iterator must be implicitly convertible to const_iterator. Since that is true, as we can see happening in insert_or_assign(), then why in C++17 was a new signature added to std::map::erase()? In C++11, we have iterator…
João Paulo
  • 6,300
  • 4
  • 51
  • 80
27
votes
6 answers

Should I prefer iterators over const_iterators?

Someone here recently brought up the article from Scott Meyers that says: Prefer iterators over const_iterators (pdf link). Someone else was commenting that the article is probably outdated. I'm wondering what your opinions are? Here is mine: One…
Frank
  • 64,140
  • 93
  • 237
  • 324
26
votes
2 answers

C++ iterator to const_iterator

How do I acquire a const_iterator (of some container class) from an iterator (of that container class) in C++? What about a const_iterator from an insert_iterator? The resulting iterator should point at the same spot as the original does.
Thomas Eding
  • 35,312
  • 13
  • 75
  • 106
21
votes
2 answers

const_iterator and constness of const_iterator::value_type

Why in STL std::iterator_traits::value_type is the same type as std::iterator_traits::value_type Why it is designed like that? Shouldn't the first be const T and the second only T? How are you supposed to take the underlying const…
Sogartar
  • 2,035
  • 2
  • 19
  • 35
17
votes
2 answers

Why const_iterator could be used with std::map::erase

I was under the impression one cant use erase on a const iterator. Check this code. Why does the below code compile (C++11, gcc)? long getMax(const bool get_new) { long max_val=0; TO now=getNow(); map&…
chrise
  • 4,039
  • 3
  • 39
  • 74
14
votes
3 answers

C++ iterator and const_iterator problem for own container class

I'm writing an own container class and have run into a problem I can't get my head around. Here's the bare-bone sample that shows the problem. It consists of a container class and two test classes: one test class using a std:vector which compiles…
BaCh
  • 625
  • 2
  • 5
  • 17
12
votes
2 answers

Why elements can be inserted in a vector when using const_iterators

Considering the code below, #include #include #include using namespace std; int main(){ vector value{22, 23, 25, 34, 99}; auto it = find(value.cbegin(), value.cend(), 25); value.insert(it, 77); …
Milo Lu
  • 3,176
  • 3
  • 35
  • 46
12
votes
1 answer

In C++17, why do associative containers have an `erase` member function that takes (non-`const`) `iterator`?

See, e.g., http://en.cppreference.com/w/cpp/container/map/erase In C++03 there were three overloads: void erase( iterator pos ); void erase( iterator first, iterator last ); size_type erase( const key_type& key ); In C++11, the first and second…
Brian Bi
  • 111,498
  • 10
  • 176
  • 312
1
2 3
10 11