iterator

Build a Basic Python Iterator

How would one create an iterative function (or iterator object) in python? ...

How to make a tree in C++?

How do I make a tree data structure in C++ that uses iterators instead of pointers? I couldn't find anything in the STL that can do this. What I would like to do is to be able to create and manipulate trees like this: #include <iostream> #include <tree> using namespace std; int main() { tree<int> myTree; tree<int>::iterator ...

How do I write a for loop in bash

I'm looking for the basic loop like: for(int i = 0; i < MAX; i++) { doSomething(i); } but for bash. ...

Assigning a Boost Xpressive token iterator range to a vector

I'm having some trouble getting Boost Xpressive to work as I expect. I'm trying to split a line of text into fields delimited by tab characters: wstring ws = L"Field1\tField2\tField3"; wsregex_token_iterator fieldIt(ws.begin(), ws.end(), as_xpr(L'\t'), -1); wsregex_token_iterator endIt; So far, so good; the above works fine. The pr...

Iterators in C++ (stl) vs Java, is there a conceptual difference?

I'm returning to c++ after being away for a bit and trying to dust off the old melon. In Java Iterator is an interface to a container having methods: hasNext(), next() and remove(). The presence of hasNext() means it has the concept of a limit for the container being traversed. //with an Iterator Iterator<String> iter = trees.iterator...

Python idiom to chain (flatten) an infinite iterable of finite iterables?

Suppose we have an iterator (an infinite one) that returns lists (or finite iterators), for example one returned by infinite = itertools.cycle([[1,2,3]]) What is a good Python idiom to get an iterator (obviously infinite) that will return each of the elements from the first iterator, then each from the second one, etc. In the example...

Returning an 'any kind of input iterator' instead of a vector::iterator or a list::iterator

Suppose I want to implement in C++ a data-structure to store oriented graphs. Arcs will be stored in Nodes thanks to STL containers. I'd like users to be able to iterate over the arcs of a node, in an STL-like way. The issue I have is that I don't want to expose in the Node class (that will actually be an abstract base class) which STL...

Is a variable named i unacceptable?

As far as variable naming conventions go, should iterators be named i or something more semantic like count? If you don't use i, why not? If you feel that i is acceptable, are there cases of iteration where it shouldn't be used? ...

Why use iterators instead of array indices?

Take the following two lines of code: for (int i = 0; i < some_vector.size(); i++) { //do stuff } And this: for (some_iterator = some_vector.begin(); some_iterator != some_vector.end(); some_iterator++) { //do stuff } I'm told that the second way is preferred. Why exactly is this? ...

Algorithm for implementing C# yield statement

I'd love to figure it out myself but I was wondering roughly what's the algorithm for converting a function with yield statements into a state machine for an enumerator? For example how does C# turn this: IEnumerator<string> strings(IEnumerable<string> args) { IEnumerator<string> enumerator2 = getAnotherEnumerator(); foreach(va...

What is the best way to obtain an index into a vector when using Iterators to loop over it.

When iterating over elements of a vector it is preferred to use iterators instead of an index (see Why use iterators instead of array indices?). std::vector<T> vec; std::vector<T>::iterator it; for ( it = vec.begin(); it != vec.end(); ++it ) { // do work } However, it can be necessary to use the index in the body of the loop. Whic...

splice() on std::list and iterator invalidation

The 3-argument form of list::splice() moves a single element from one list to the other. SGI's documentation explicitly states that all iterators, including the one pointing to the element being moved remain valid. Roguewave's documentation does not say anything about iterator invalidation properties of splice() methods, whereas the C+...

C++ Creating my own Iterators

I'm trying to learn C++ so forgive me if this question demonstrates a lack of basic knowledge, you see, the fact is, I have a lack of basic knowledge. I want some help working out how to create an iterator for a class I have created. I have a class 'Shape' which has a container of Points. I have a class 'Piece' which references a Shap...

C++ last loop iteration (STL map iterator)

I'm trying to figure out the best way to determine if I'm in the last iteration of a loop over a map in order to do something like the following: for (iter = someMap.begin(); iter != someMap.end(); ++iter) { bool last_iteration; // do something for all iterations if (!last_iteration) { // do something for all but the...

How can I expose iterators without exposing the container used?

Hello world! I have been using C# for a while now, and going back to C++ is a headache. I am trying to get some of my practices from C# with me to C++, but I am finding some resistance and I would be glad to accept your help. I would like to expose an iterator for a class like this: template <class T> class MyContainer { public: /...

Is it safe to increase an iterator inside when using it as an argument?

Currently I'm trying to erase a sequence of iterators from a set, however GCC's standard library seems to be broken because std::set::erase(iterator) should return the an iterator (next iterator), however in GCC it returns void (which is standard?) Anyways I want to write: myIter = mySet.erase(myIter); But GCC doesn't like it... So I...

STL non-copying wrapper around an existing array?

Is it possible to create an STL-like container, or even just an STL-style iterator, for an existing array of POD-type elements? For example, suppose I have an array of ints. It would be convenient to be able to call some of the STL functions, such as find_if, count_if, or sort directly on this array. Non-solution: copying the entire a...

Iterators.. why use them?

In the STL library some containers have iterators and it is commonly held that they are a superior way of iterating through these containers rather than simple for loops e.g. for ( int i=0; i < vecVector.size(); i++ ) { .. } Can anyone tell me why and in what cases I should use iterators and in what cases the code snippet above plea...

How do you iterate backwards through an STL list ?

I'm writing some cross-platform code between Windows and Mac. If list::end() "returns an iterator that addresses the location succeeding the last element in a list" and can be checked when traversing a list forward, what is the best way to traverse backwards? This code workson the Mac but not on Windows (can't decrement beyond first e...

Is Iterator initialization inside for loop considered bad style, and why?

Typically you will find STL code like this: for (SomeClass::SomeContainer::iterator Iter = m_SomeMemberContainerVar.begin(); Iter != m_SomeMemberContainerVar.end(); ++Iter) { } But we actually have the recommendation to write it like this: SomeClass::SomeContainer::iterator Iter = m_SomeMemberContainerVar.begin(); SomeClass::SomeCont...