iterator

How do you return a vector iterator from a variable in a templated class?

I'm trying to return an iterator for a vector in a templated class (I'm not sure if that makes a difference, but I've read that may, so I thought I'd mention it). The problem is that I get an error about C++ not supporting default-int when I try this. I've looked online and from what I can see in forums and explanaions, I don't think I...

Writing custom IEnumerator<T> with iterators

How can I write a custom IEnumerator<T> implementation which needs to maintain some state and still get to use iterator blocks to simplify it? The best I can come up with is something like this: public class MyEnumerator<T> : IEnumerator<T> { private IEnumerator<T> _enumerator; public int Position {get; private set;} // or some ...

Is there any way to check if an iterator is valid?

For two threads manipulating a container map for example, what the correct way to test whether an iterator still valid (for performance reason) ? Or would be of only indirect way that this can be done. The sample code for this : #define _SECURE_SCL 1 //http://msdn2.microsoft.com/en-us/library/aa985973.aspx #define _SECURE_SCL_THROWS 1 ...

Implementing a bidirectional enumerator in C#

Is there a way to use yield blocks to implement an IEnumerator<T> which can go backward (MoveLast()) as well as forward? ...

How do I iterate over/dereference an array of subroutine refs in Perl?

I’m trying to figure out how to iterate over an array of subroutine refs. What’s wrong with this syntax? use strict; use warnings; sub yell { print "Ahh!\n"; } sub kick { print "Boot!\n"; } sub scream { print "Eeek!\n"; } my @routines = (\&yell, \&kick, \&scream); foreach my $routine_ref (@routines) { my &routine = &{$routine_ref};...

When should I use IEnumerator for looping in c#?

I was wondering if there are any times where it's advantageous to use an IEnumerator over a foreach loop for iterating through a collection? For example, is there any time where it would be better to use either of the following code samples over the other? IEnumerator<MyClass> classesEnum = myClasses.GetEnumerator(); while(classesEnum.M...

Is there a Perl equivalent of Python's re.findall/re.finditer (iterative regex results)?

In Python compiled regex patterns have a findall method that does the following: Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found. If one or more groups are present in the pattern, return a list of groups; this ...

Using an STL Iterator without initialising it

I would like to do something like this: container::iterator it = NULL; switch ( eSomeEnum ) { case Container1: it = vecContainer1.begin(); break; case Container2: it = vecContainer2.begin(); break; ... } for( ; it != itEnd ; ++it ) { .. } But I can't create and initialise an iterator to NULL. Is there some way I can do this? Ide...

A C++ iterator adapter which wraps and hides an inner iterator and converts the iterated type

Having toyed with this I suspect it isn't remotely possible, but I thought I'd ask the experts. I have the following C++ code: class IInterface { virtual void SomeMethod() = 0; }; class Object { IInterface* GetInterface() { ... } }; class Container { private: struct Item { Object* pObject; [... other m...

error: conversion from long int to non-scalar type, comparing an iterator to null

Hello I hope someone can explain this problem. This is the code: class Memory{ public: PacketPtr pkt; MemoryPort* port; MemCtrlQueueEntry(){}; }; And after I do: std::list<Memory*>::iterator lastIter = NULL; And I get the following error: error: conversion from long int to non-scalar type std::_List_iterator<DRAMMemor...

select single item from a collection : Python

I created a utility function to return the expected single item from an generator expression print one(name for name in ('bob','fred') if name=='bob') Is this a good way to go about it? def one(g): try: val = g.next() try: g.next() except StopIteration: return val else: ...

C++ Tokenizing using iterators in an eof() cycle

Hi, I'm trying to adapt this answer http://stackoverflow.com/questions/53849/how-do-i-tokenize-a-string-in-c#53921 to my current string problem which involves reading from a file till eof. from this source file: Fix grammatical or spelling errors Clarify meaning without changing it Correct minor mistakes I want to create a vect...

How do I alter this tokenization process to work on a text file with multiple lines?

Hi; I'm working this source code: #include <string> #include <vector> #include <iostream> #include <istream> #include <ostream> #include <iterator> #include <sstream> #include <algorithm> int main() { std::string str = "The quick brown fox"; // construct a stream from the string std::stringstream strstr(str); // use stream it...

Keys / Values Functionality to Iterators in C++

Hi, I know this questions has come up in various guises before, but this is slightly different. I have a class which contains a std::map. Although I wish to use the map for other purposes inside the class, externally I want to expose an iterator adapter to just the values inside the map (ie the second item in the std::pair). For examp...

Iterating over bidimensional Java arrays.

public static List<Vertex<Integer>> petersenGraph() { List<Vertex<Integer>> v = new ArrayList<Vertex<Integer>>(); for (int i = 0; i < 10; i++) { v.add(new Vertex<Integer>(i)); } int[][] edges = {{0,1}, {1,0}, {1,2}, {2,1}, {2,3}, {3,2}, {3,4}, {4,3}, {4,0}, {0,4}, {5,6}, {6,5}, {6,7}, {7,6}, {7,8}, {...

Does a method that returns a collection get called in every iteration in a foreach statement in C#?

Hi, I was working with some C# code today in the morning and I had something like: foreach(DataRow row in MyMethod.GetDataTable().Rows) { //do something } So, as I dont have a full understanding of the language framework I would like to know if GetDataTable() gets called each time an iteration is done or if it just gets called once an...

What basic operations on a Map are permitted while iterating over it?

Say I am iterating over a Map in Java... I am unclear about what I can to that Map while in the process of iterating over it. I guess I am mostly confused by this warning in the Javadoc for the Iterator interface remove method: [...] The behavior of an iterator is unspecified if the underlying collection is modified while the iterati...

Refactor this Python code to iterate over a container

Surely there is a better way to do this? results = [] if not queryset is None: for obj in queryset: results.append((getattr(obj,field.attname),obj.pk)) The problem is that sometimes queryset is None which causes an exception when I try to iterate over it. In this case, I just want result to be set to an empty list. This co...

How to iterate over two arrays at once?

I have two arrays built while parsing a text file. The first contains the column names, the second contains the values from the current row. I need to iterate over both lists at once to build a map. Right now I have the following: var currentValues = currentRow.Split(separatorChar); var valueEnumerator = currentValues.GetEnumerator(); ...

Modifying a set during iteration java

I'm looking to make a recursive method iterative. I have a list of Objects I want to iterate over, and then check their subobjects. Recursive: doFunction(Object) while(iterator.hasNext()) { //doStuff doFunction(Object.subObjects); } I want to change it to something like this doFunction(Object) iIterator = hashSet.iterator(); ...