Are non dereferenced iterators past the “one past-the-end” iterator of an array undefined behavior?

Yes, your program has undefined behaviour if you form such a pointer. That’s because the only way you can do so is to increment a valid pointer past the bounds of the object it points inside, and that is an undefined operation. [C++14: 5.7/5]: When an expression that has integral type is added to or … Read more

Is an iterator in C++ a pointer?

The short answer is: Pointer is a kind of iterator. Pointer can be therefore used as an iterator. Pointer has properties other than iterator. History Historically, we have C pointer, and it is adapted into C++ when C++ is invented. Pointer represents a location in memory, therefore can be used as a location in an … Read more

Why does a push_back on an std::list change a reverse iterator initialized with rbegin?

The standard says that iterators and references remain valid during an insert. It doesn’t say anything about reverse iterators. 🙂 The reverse_iterator returned by rbegin() internally holds the value of end(). After a push_back() this value will obviously not be the same as it was before. I don’t think the standard says what it should … Read more

Does resizing a vector invalidate iterators?

Edited with more careful wording yes, resizing a vector might invalidate all iterators pointing into the vector. The vector is implemented by internally allocating an array where the data is stored. When the vector grows, that array might run out of space, and when it does, the vector allocates a new, bigger, array, copies the … Read more

Some help understanding “yield”

A method using yield return must be declared as returning one of the following two interfaces: IEnumerable<SomethingAppropriate> IEnumerator<SomethingApropriate> (thanks Jon and Marc for pointing out IEnumerator) Example: public IEnumerable<AClass> YourMethod() { foreach (XElement header in headersXml.Root.Elements()) { yield return (ParseHeader(header)); } } yield is a lazy producer of data, only producing another item after the … Read more

How to select a random element in std::set?

You could use the std::advance method. #include <set> #include <algorithm> int main() { using namespace std; // generate a set… set<int> s; for( int i = 0; i != 10; ++i ) s.insert(i); auto r = rand() % s.size(); // not _really_ random auto n = *select_random(s, r); } Where template<typename S> auto select_random(const S … Read more

How can I implement the Iterable interface?

Iterable is a generic interface. A problem you might be having (you haven’t actually said what problem you’re having, if any) is that if you use a generic interface/class without specifying the type argument(s) you can erase the types of unrelated generic types within the class. An example of this is in Non-generic reference to … Read more

How to avoid code duplication implementing const and non-const iterators?

[The best answer was, unfortunately, deleted by a moderator because it was a link-only answer. I understand why link-only answers are discouraged; deleting it, however, has robbed future seekers of very useful information. The link has remained stable for more than seven years and continues to work at the time of this writing.] I strongly … Read more

tech