iterator

improved collection Iterator

Hi, Personally, I find the range of functionality provided by java.util.Iterator to be fairly pathetic. At a minimum, I'd like to have methods such as: peek() returns next element without moving the iterator forward previous() returns the previous element Though there are lots of other possibilities such as first() and last(). Does...

list iterator not incrementable

I have an old project that was built using visual studio 2003 and I recompiled it with vs2005 recently. However, during runtime, I get the following error: list iterator not incrementable I traced the program to this function: void InputQueue::update() { list<PCB>::iterator iter; list<PCB>::iterator iterTemp; for(iter = b...

Should an object implement an iterator or contain another object that implements an iterator

I'm trying to get my head around SPL iterators and I've come up with 2 ways to handle it. I see the first version to be less complicated but the second version has composition feel to it (I think). What am I not seeing is which one is preferable over the other? Or am I just over complicating this? Here are my thoughts: The object impl...

What is a safe equivalent of non-void STL erase?

Suppose I have a hash_map and a code like // i is an iterator i = hash_map.erase(i) But GCC's STL doesn't return iterator in erase, but a void. Now is a code like hash_map.erase(i++) safe (i.e. does not invalidate the iterator or does any other unexpected or unpleasant things)? Please note this is a hash_map. ...

How do I merge two python iterators?

I have two iterators, a list and an itertools.count object (i.e. an infinite value generator). I would like to merge these two into a resulting iterator that will alternate yield values between the two: >>> import itertools >>> c = itertools.count(1) >>> items = ['foo', 'bar'] >>> merged = imerge(items, c) # the mythical "imerge" >>> m...

When is not a good time to use python generators?

This is rather the inverse of What can you use Python generator functions for?: python generators, generator expressions, and the itertools module are some of my favorite features of python these days. They're especially useful when setting up chains of operations to perform on a big pile of data--I often use them when processing DSV fil...

How to read arbitrary number of values using std::copy?

Hi, I'm trying to code opposite action to this: std::ostream outs; // properly initialized of course std::set<int> my_set; // ditto outs << my_set.size(); std::copy( my_set.begin(), my_set.end(), std::ostream_iterator<int>( outs ) ); it should be something like this: std::istream ins; std::set<int>::size_type size; ins >> size; s...

Iterator Pattern VB.net (C# would use yield!)

Hello, I want to implement the iterator pattern in VB.net, which does not have the yield keyword. Any ideas or links please? ...

Sorting PHP Iterators

Is there a simple way to sort an iterator in PHP (without just pulling it all into an array and sorting that). The specific example I have is a DirectoryIterator but it would be nice to have a solution general to any iterator. $dir = new DirectoryIterator('.'); foreach ($dir as $file) echo $file->getFilename(); I'd like to be abl...

iterator adapter to iterate just the values in a map?

I'm just getting back into C++ after a couple of years of doing a lot of C#, and recently Objective C. One thing I've done before is to roll my own iterator adapter for std::map that will deref to just the value part, rather than the key-value pair. This is quite a common and natural thing to do. C# provides this facility with its Keys ...

Compare two consecutive elements in std::list

I'd like to compare two consecutive elements in a std::list while iterating through the list. What is the proper way to access element i+1 while my iterator is at element i? Thanks Cobe ...

What happens if you call erase() on a map element while iterating from begin to end?

In the following code I loop through a map and test if an element needs to be erased. Is it safe to erase the element and keep iterating or do I need to collect the keys in another container and do a second loop to call the erase()? map<string, SerialdMsg::SerialFunction_t>::iterator pm_it; for (pm_it = port_map.begin(); pm_it != port_...

Is it possible to rewind a PDO result?

I'm trying to write an iterator for results from a PDO statement but I can't find any way of rewinding to the first row. I would like to avoid the overhead of calling fetchAll and storing all the result data. // first loop works fine foreach($statement as $result) { // do something with result } // but subsequent loops don't foreac...

STL Migration issues (VS 2003 -> 2005)

I have just converted a project from Visual Studio 2003 to 2005 and although most of it 'converted' fine, I have a series of STL errors from the following line: void SomeFn( std::vector<CSomeObject*>::iterator it, std::vector<CSomeObject*>::iterator itBegin = NULL, std::vector<CSomeObject*>::iterator itEnd = NULL ); The Visual Studio ...

STL::Map - Walk through list or use find?

Say I have a method that needs to pull 8 values from a map with 100 elements in it. Which do you think would be preferable: Walk in a for loop from begin to end once, pulling the elements out by switching on the key? Or using find 8 times to get those values? ...

Is Yield Return == IEnumerable & IEnumerator?

I just want to verify, is yield return a shortcut for implementing IEnumerable and IEnumerator? Thanks, John ...

last key in a std::map

I am looking for the highest key value (a defined by the comparison operator) of a std::map. Is this guaranteed to be map.rbegin()->first ? (I am a bit shaky on reverse iterators, and how much freedom there is in the implementation of std::map) If not, please advise. I cannot change the data structure. ...

is there a merged iterator implementation?

Is there an Iterator implementation that merges multiple iterators? class MergedIterator<T> implements Iterator<T> { MergedIterator(Iterator<T>... iters) .... } And the next method should move on to iters[1] when !iters[0].hasNext() etc ...

const and nonconst STL Iterators

What is the difference between a ::const_iterator and an ::iterator and where would you use one over the other? ...

C++: Returning an Iterator

I have a function which searches an STL container then returns the iterator when it finds the position, however I am getting some funny error messages, can tell me what I am doing wrong? Function: std::vector< CClass >::iterator CClass::SearchFunction( const std::string& strField ) { ... return it; ... } Error: error C2664: 'st...