iterator

Iterate over the lines of a string

I have a multi-line string defined like this: foo = """ this is a multi-line string. """ This string us used as test-input for a parser I am writing. The parser-function receives a file-object as input and iterates over it. It does also call the next() method directly to skip lines, so I really need an iterator as input, not an itera...

Java Flow Control Problem

I am programming a simple 2d game engine. I've decided how I'd like the engine to function: it will be composed of objects containing "events" that my main game loop will trigger when appropriate. A little more about the structure: Every GameObject has an updateEvent method. objectList is a list of all the objects that will receive upd...

Why does string::find return size_type and not an iterator?

In C++, why does string::find return size_type and not an iterator? It would make sense because functions like string::replace or string::insert take iterators as input, so you could find some character and immediately pass the returned iterator to replace, etc. Also, std::find returns an iterator -- why is std::string::find different...

Joining the previous and next sentence using python

I'm trying to join a set of sentences contained in a list. I have a function which determines whether a sentence in worth saving. However, in order to keep the context of the sentence I need to also keep the sentence before and after it. In the edge cases, where its either the first or last sentence then, I'll just keep the sentence a...

Java - HashSet best way to implement iterator that does not support remove()

Hey, I have a class which uses a HashSet and I want the class implement Iterable, I do not, however, want the class iterator to support the remove() method. The default iterator of HashSet is HashSet.KeyIterator which is a private class within the HashSet class, so I cannot simply extend it and override the remove method. Ideally ...

Troubles with STL list iterators

After a thousand of attempts to Google/bypass that using pointers, I decided myself to post this question to you guys. Here's the problem: I'm trying to create a minimum spanning tree from a graph that I previously made. I'm using Kruscal's Algorithm for that, which is about checking every arc from the shorter to the longest and taking ...

Flexible mutation of sequence containers in C++

I'm pondering a current limitation of STL iterators and wondering if there's an elegant way around it. Here's my situation: I have a class that encapsulates a sequence container and a generic method to mutate the contents of the container, for example: class Container { typedef std::vector<int> Data; Data data_; public: template ...

Custom iterator: how do I keep track of it?

I have this situation: I have a class which keeps track of an array of pointers. I built a custom iterator which loops through this array. My problem is on how to make it threadsafe, especially while incrementing/decrementing? Here is a draft of the relevant parts of what I have: typedef fruit * iterator; class fruits { private: ...

How to use for_each with the function as the overloaded operator()

I have a std::vector of function objects. Each object can take an int, so I can say obj(4) and get an int result. How can I use the algorithm for_each to work on each element of the vector? ...

Caching the end iterator - Good idea or Bad Idea?

Generally speaking is it a good idea to cache an end iterator (specifically STL containers) for efficiency and speed purposes? such as in the following bit of code: std::vector<int> vint; const std::vector<int>::const_iterator end = vint.end(); std::vector<int>::iterator it = vint.begin(); while (it != end) { .... ++it; } Under...

What do Iterator and Iterable mean?

What are the meanings of "iterator" and "iterable", and what are the benefits of using them. Please answer as I have an exam tomorrow. ...

C++ iterator in for loop pitfalls?

I see somewhere it metions: for ( itr = files.begin(); itr < files.end(); ++itr ) // WRONG for ( itr = files.begin(); itr != files.end(); ++itr ) // ok Why is the first expression wrong? I always used the first expression, and didn't have any problems. ...

iterator_traits: code from Effective C++, Item 47 and TCPPSL, Section 7.5.1

This is going to be rather long, so I apologize preemptively. Near the end of Item 47 in Scott Meyers's "Effective C++" (Third Edition) he shows the following code: template<typename IterT, typename DistT> void advance(IterT& iter, DistT d) { doAdvance( iter, d, typename std::iterator_traits<IterT>::...

One-liner to check whether an iterator yields at least one element?

Currently I'm doing this: try: something = iterator.next() # ... except StopIteration: # ... But I would like an expression that I can place inside a simple if statement. Is there anything built-in which would make this code look less clumsy? any() returns False if an iterable is empty, but it will potentially iterate ove...

Ruby elegant way to deal card array to player objects

I'm having trouble finding a nice elegant ruby way to do this. I have a deck array with 52 Card objects, and I want to iterate over it and loop through an array of Player objects, dealing one card at a time into their hand array. Something like: deck = Card.deck.shuffle! deck.each do |card| @players.NEXTELEMENT.hand << card end wh...

(Beginner's question) Echoing through an array in PHP, only adding a comma based on certain conditions

Hi folks, So this is pretty basic, I know! Sorry, just one of those days. I've got an array of tags collected from a database. There could be any number of tags in this array. However, I only want to output 4. So I currently have this code: $iteration = 0; foreach ($tagarray as $tag) { ?> <div class="tagbutton listv"> <span><?php ech...

How to iterate through a MySQL table with Python?

Hi, I have a Python script which uses the MySQLdb interface to load various CSV files into MySQL tables. In my code, I use Python's standard CSV library to read the CSV, then I insert each field into the table one at a time, using an INSERT query. I do this rather than using LOAD DATA so that I can convert null values and other minor c...

accessing C# base class in iterator causes ReSharper warning

I have two classes, GenericList and SpecificList, where SpecificList inherits from GenericList. GenericList implements IEnumerable<GenericItem> and SpecificList implements IEnumerable<SpecificItem>. SpecificItem inherits from GenericItem. I have to implement GetEnumerator in both GenericList and SpecificList since they implement IEnum...

Python: If an iterator is an expression, is it calculated every time?

Take the following example: >>> for item in [i * 2 for i in range(1, 10)]: print item 2 4 6 8 10 12 14 16 18 Is [i * 2 for i in range(1, 10)] computed every time through the loop, or just once and stored? (Also, what is the proper name for that part of the expression?) One reason I would want to do this is that I only want the ...

Best way to iterate over two lists simultaneously?

Suppose I have this: public class Unit<MobileSuit, Pilot> { ... List<MobileSuit> mobileSuits; List<Pilot> pilots; ... } And I would like to iterate through the pair of each in the simplest way outside of that class. How should I go about doing that? I thought about doing this: public class Unit<MobileSuit, Pilot> ...