Skip to main content
21 votes
Accepted

C++ Iterator, Why is there no Iterator base class all iterators inherit from

You've already gotten answers pointing to why it's not necessary for all iterators to inherit from a single Iterator base class. I'd got quite a bit further though. One of the goals of C++ is ...
Jerry Coffin's user avatar
  • 44.9k
16 votes

pre-increment vs. post-increment

You may be right regarding pointers. However: C is not C++. C++ guarantees some very precise semantics especially around when copies are made, as this affects RAII: in C++, copies are an observable ...
amon's user avatar
  • 136k
10 votes

C++ Iterator, Why is there no Iterator base class all iterators inherit from

The difference is between What something is, and How something behaves. A lot of languages try to conflate the two together, but they are quite distinct things. If How is What, and What is How... ...
Kain0_0's user avatar
  • 16.6k
7 votes

C++ Iterator, Why is there no Iterator base class all iterators inherit from

Because C++ doesn't need to have (abstract) base classes to do polymorphism. It has structural subtyping as well as nominative subtyping. Confusingly in the particular case of Iterators, previous ...
Caleth's user avatar
  • 12.4k
6 votes

C++ Iterator, Why is there no Iterator base class all iterators inherit from

One reason is that iterators don't have to be instances of a class. Pointers are perfectly good iterators in many cases, for example, and since those are primitives they can't inherit from anything.
David Thornley's user avatar
6 votes

Should I move tasks which is just for a specific element only out of for loop?

There is a huge difference in both code snippets: if this.arr.length is 0 then option 1 works as designed, whereas option 2 fails in an attempt to perform operations on not existing elements. ...
Christophe's user avatar
  • 82.3k
6 votes

Is it true that "A Java Iterator is an Abstract Data Type"?

A Tale of Abstractions There's a lot of confusion about this on the Internet, but the term is quite technical, and according to the Cook paper (referenced in the answer liked to by Greg Burghardt), it ...
Filip Milovanović's user avatar
5 votes
Accepted

Hash Table with iterators as the keys, is this poor design and can I do this better?

Algorithm So, AFAICT your algorithm boils down to: For each new_item, find the closest old_item. (This creates a set of old_items.) For each old_item in the set, update it with the closest new_item. ...
hoffmale's user avatar
  • 777
5 votes

using-declaration or typedef for iterator tags?

In C++, using and typedef are mostly equivalent and both declare a type alias. But they have reverse order of arguments: typedef original new_name; using new_name = original; The typedef can get very ...
amon's user avatar
  • 136k
5 votes

JS - two array filters vs. one forEach?

This might be a case of pre-optimization. Each call to filter will loop over the array, so your first example iterates twice. Your second example iterates once. On the surface your second approach ...
Greg Burghardt's user avatar
5 votes
Accepted

Should I always use iterators when working with strings?

Go for what'll be least surprising to yourself and your colleagues in the future. I've heard this termed the principle of least surprise, and it's a pretty simple idea. If there's a simple way to ...
Alecto's user avatar
  • 571
4 votes
Accepted

Indexable iterators

Indexing should be free of side effects. That is, I expect x[a] == x[a] for all objects x and all valid indices a. This rules out solutions 1a and 2a. Reasoning about iterator indexing in 1a sounds ...
amon's user avatar
  • 136k
4 votes
Accepted

How to Implement a `function` with `return` Without Using the `function` keyword

If you are writing an interpreter for an machine code instruction set architecture, you provide for and maintain (1) the CPU's registers, which includes the Program Counter (aka Instruction Pointer), ...
Erik Eidt's user avatar
  • 34.8k
4 votes

Iterator/Range design, can a lightweight input iterator implemented with C++/java's iterator model?

what are the cons of Java's access+increment iterator model (can you give an example of that)? Well, there is the obvious: the fact that you cannot increment without accessing. Let's say that you ...
Nicol Bolas's user avatar
  • 12.1k
4 votes
Accepted

Is it bad practice to do additional work in IAsyncEnumerable generator method?

I think this is less about IAsyncEnumerable and more about good OOP design. Mainly the rule of least surprise and high cohesion. I would never expect method named GetRowsAsync to do anything like ...
Euphoric's user avatar
  • 38.2k
3 votes

using-declaration or typedef for iterator tags?

These aren't using-declarations. They are alias-declarations. Aside from the different syntax, they are 100% semantically identical to typedefs. I find them a lot more readable, though, and I imagine ...
Sebastian Redl's user avatar
3 votes

JS - two array filters vs. one forEach?

I know that the Array prototype method filter is generally preferred over forEach What makes you say that? Filters and forEach are two different things; they are not interchangeable. There are ...
Arseni Mourzenko's user avatar
3 votes
Accepted

How much an iterator should do

Different iterators provide different contracts. C++ probably has the most fine-grained iterator concepts, distinguishing operators that can move forward can move backwards can jump to another element ...
amon's user avatar
  • 136k
2 votes

Make lambdas concise using enumerations?

In Java, the idiomatic way to group a bunch of functions is just a plain old class with plain old methods. Then your logFormatList looks something like List.of(LogFormatFunctions::date, ...
Karl Bielefeldt's user avatar
2 votes
Accepted

How to implement a cursor iterator?

The correct approach for the cursor is option 2 The answer is already in your quote, which describes option 2: (...) We call this kind of iterator a cursor, since it merely points to the ...
Christophe's user avatar
  • 82.3k
2 votes

Indexable iterators

Here's something to consider; in terms of API-design, an iterator is an abstraction of a traversal strategy. So, calling next() doesn't have to do "a, b, c, d, e, f, g, h, i, j", it could do ...
Filip Milovanović's user avatar
1 vote

How to implement a cursor iterator?

tl;dr: use your language's recommended pattern. The dilemma between iterator methods belonging to the aggregate or to the iterator looks like a classical pseudo-question of "object oriented design" ...
max630's user avatar
  • 2,605
1 vote

Indexable iterators

PEP 234 says A class that wants to be an iterator should implement two methods: a next() method that behaves as described above, and an iter() method that returns self. The two methods correspond to ...
lennon310's user avatar
  • 3,242
1 vote

Is it true that "A Java Iterator is an Abstract Data Type"?

So, may I state that an Iterator is an abstract data type? An ADT is a collection of data and a set of operations that may be performed on that data. An iterator is composed of a set of data to ...
David Arno's user avatar
  • 39.6k
1 vote

Should I always use iterators when working with strings?

The primary reason for iterators is to allow/support generic algorithms that can work with containers of different sorts--arrays, trees, linked lists, etc. The iterator decouples the algorithmic part (...
Jerry Coffin's user avatar
  • 44.9k
1 vote

Should I always use iterators when working with strings?

Well, there are functional differences between the examples: The index example can deal with the string being arbitrarily modified during the loop. It buys that by always going back to it, and re-...
Deduplicator's user avatar
  • 9,329

Only top scored, non community-wiki answers of a minimum length are eligible