FAQ Database Discussion Community


Iterator invalidation by `std::string::begin()`/`std::string::end()`?

c++,c++11,iterator,c++98,const-iterator
#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.end(); std::cout << end - begin << '\n'; } This code mixes the result of begin() const with the result of end(). Neither of these functions...

Should the regular iterator (or a similar range / view class) derive from const_iterator?

c++,design-patterns,iterator,const-iterator
Is it a good practice to make the regular iterator (or a similar range or view class) derive from the corresponding const_iterator? The motivation for this is sharing the code, and avoiding conversions, when the iterator / view is big. As an example, consider a view of a 3D grid,...

Const-correctness of self made iterators

c++,iterator,const-iterator
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_reverse_iterator. In this example, I will only pay attention on iterator and const_iterator, the two others are very similar. After that, I...

Cast object into vector's const_iterator

c++,vector,iterator,const-iterator
Hi I have a function which takes std::vector<T>::const_iterator as parameter. Question is: How can I cast standalone object of type T, into iterator, so I can use it as an argument?...

Can assignment from a const_iterator dereference cause undefined behaviour?

c++,pointers,pass-by-reference,undefined-behavior,const-iterator
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. #include <iostream> #include <list> using namespace std; typedef int* intp; typedef std::list<intp> intplist;...