const-iterator Questions

10

Solved

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 reg...
Floriated asked 27/8, 2010 at 8:50

10

Solved

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 poin...
Culhert asked 19/4, 2009 at 9:54

4

Solved

I have written following code snippet but it does not seem to be working. int main(){ int VCount, v1, v2; pair<float, pair<int,int> > edge; vector< pair<float, pair<int,int...
Ceroplastic asked 1/9, 2014 at 12:41

3

Solved

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,...
Gadroon asked 7/3, 2012 at 11:31

7

Solved

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?
Noni asked 17/8, 2012 at 7:16

8

Solved

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 nee...
Nonparous asked 8/11, 2011 at 17:6

1

Let's say I'm implementing a collection, say something like std::vector. I need to implement iterator and const_iterator, but once I've made iterator can const_iterator not just be implemented as i...
Talented asked 17/6, 2019 at 1:19

1

Solved

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 adde...
Conclusion asked 8/6, 2018 at 16:26

2

Solved

I have this piece of code: auto it = my_map.lower_bound(my_key); The following assert gives me error: static_assert(std::is_same<decltype(it), std::map<K, V>::const_iterator>::value...
Gallicism asked 6/6, 2018 at 17:23

2

C++98 containers defined two kinds of iterator, ::iterators and ::const_iterators. Generally, like this: struct vec{ iterator begin() ; const_iterator begin() const; }; In C++11 this part of the...
Amorous asked 21/2, 2018 at 21:37

2

Solved

Considering the code below, #include <iostream> #include <algorithm> #include <vector> using namespace std; int main(){ vector<int> value{22, 23, 25, 34, 99}; auto it = ...
Pozsony asked 11/2, 2018 at 3:27

6

Solved

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...
Fossil asked 19/4, 2009 at 11:28

2

Solved

Let say I define some kind of container A: struct A { iterator begin(){ return iterator(this,0); } const iterator cbegin() const { return iterator(this, last());} //... }; Suppose now I want ...
Allelomorph asked 3/7, 2017 at 9:43

3

Solved

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 prefe...
Parnassus asked 18/3, 2011 at 0:45

1

Solved

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( cons...
Galloglass asked 21/12, 2016 at 6:47

2

Solved

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...
Taxexempt asked 21/12, 2016 at 5:13

1

Solved

The comment at Why does boost::find_first take a non-const reference to its input? suggests "the caller to create a non-const iterator_range with const_iterator template parameter to "prove" that t...
Doublebreasted asked 1/4, 2016 at 20:23

3

Solved

Consider the following code: #include <vector> #include <iostream> int main() { std::vector<int> vec{1,2,3,5}; for(auto it=vec.cbegin();it!=vec.cend();++it) { std::cout <...
Prefigure asked 14/2, 2016 at 10:43

1

Solved

I am trying to write a class that should act as a sorted view on some underlying sequence of elements. So far I have come up with a non-const version. Now I have problems adapting it to also provid...
Nobe asked 8/10, 2015 at 15:48

2

Solved

This code is a simplified test for something I am trying to do for real elsewhere. I have a function which takes a "ref-to-ptr" argument and modifies it to return a pointer from a list of pointers....

2

Solved

General goal I manage a collection of objects (Collection of Real as simple example). Then I defined iterators on my collection. That means : iterator, const_iterator, reverse_iterator and const_r...
Meadors asked 8/6, 2015 at 14:7

2

Solved

Here is my code: #include <set> #include <iostream> using namespace std; int main(){ set<int> st; st.insert(1); int x = st.find(1) - st.begin(); return 0; } I am getting ...
Electrolysis asked 21/4, 2015 at 6:12

4

Solved

#include <string> #include <iostream> int main() { std::string s = "abcdef"; std::string s2 = s; auto begin = const_cast<std::string const &>(s2).begin(); auto end = s2...
Flange asked 26/2, 2015 at 17:1

3

// Cat.h class Cat {public: void const_meow() const{ ... }; void meow(){ ... }; }; class CatLibrary {public: std::vector<std::shared_ptr<Cat>>::iterator begin() { return m_cat_...
Thorlay asked 6/8, 2014 at 6:7

2

Solved

With the advent of C++11, we have unordered_map.cbegin/cend to specifically return us values of const_iterator. so the deduced type of 'it' in the expression "auto it = unordered_map.cbegin()" is c...
Bracteole asked 21/8, 2013 at 18:41

© 2022 - 2025 — McMap. All rights reserved.