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…
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…
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?
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.
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…
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…
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
…
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…
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…
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.
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…
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&…
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…
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…