Are there equivalents to slice::chunks/windows for iterators to loop over pairs, triplets etc?

It’s possible to take chunks of an iterator using Itertools::tuples, up to a 4-tuple: use itertools::Itertools; // 0.9.0 fn main() { let some_iter = vec![1, 2, 3, 4, 5, 6].into_iter(); for (prev, next) in some_iter.tuples() { println!(“{}–{}”, prev, next); } } (playground) 1–2 3–4 5–6 If you don’t know that your iterator exactly fits into … Read more

C++ template typename iterator

In list<tNode<T>*>::iterator, you have a dependant name, that is, a name that depends on a template parameter. As such, the compiler can’t inspect list<tNode<T>*> (it doesn’t have its definition at this point) and so it doesn’t know whether list<tNode<T>*>::iterator is either a static field or a type. In such a situation, the compiler assumes that … Read more

C++ iterators considered harmful?

First, to answer your questions: No. In fact, I argued elsewhere that iterators are the most important/fundamental concept of computer science ever. I (unlike Andrei) also think that iterators are intuitive. Yes, definitely but that shouldn’t come as a surprise. Hmm. Looking at Boost.Range and C++0x – haven’t they already? Andrei’s big contribution here is … Read more

Difference between Iterator and Spliterator in Java8

An Iterator is a simple representation of a series of elements that can be iterated over. eg: List<String> list = Arrays.asList(“Apple”, “Banana”, “Orange”); Iterator<String> i = list.iterator(); i.next(); i.forEachRemaining(System.out::println); #output Banana Orange A Spliterator can be used to split given element set into multiple sets so that we can perform some kind of operations/calculations on … Read more

Is it safe to delete elements in a Set while iterating with for..of?

Yes, it is perfectly fine to add elements and remove elements to a set while iterating it. This use case was considered and is supported in JavaScript 2015 (ES6). It will leave it in a consistent state. Note this also applies to itearting with forEach. Intuitively: The set iteration algorithm basically looks something like this: … Read more

Types of iterator : Output vs. Input vs. Forward vs. Random Access Iterator

If you can, find and read “The C++ Standard Library: A Tutorial and Reference”. This book contains a whole chapter about STL iterators. Here is a little something from the book: Iterator Category Ability Providers —————– ——————————- —————————- Input iterator Reads forward istream Output iterator Writes forward ostream, inserter Forward iterator Reads/writes forward forward_list, unordered_[multi]set, … Read more