iterator

How do I get a keyIterator for a LinkedHashMap?

By looking at the source code for LinkedHashMaps from Sun, I see that there is a private class called KeyIterator, I would like to use this. How can I gain access? ...

Immutable Objects in Java and Accessing Data

I've implemented a class in Java which, internally, stores a List. I want the class to be immutable. However, I need to perform operations on the internal data which don't make sense in the context of the class. Hence, I have another class which defines a set of algorithms. Here is a simplified example: Wrapper.java import java.uti...

Does an STL map always give the same ordering when iterating from begin() to end()?

It appears to from my simple testing but I'm wondering if this is guaranteed? Are there conditions where the ordering will be not be guaranteed? Edit: The case I'm particularly interested in is if I populate a map with a large number of entries, will the order of the itertator be the same across multiple runs of my executable? What if ...

Deleting items in foreach

Should you be allowed to delete an item from the collection you are currently iterating in a foreach loop? If so, what should be the correct behavior? ...

chain iterator for C++

Python's itertools tools implements a chain iterator which essentially merges a number of different iterators into a single one. Is there something similar in C++ ? A quick look at the boost libraries didn't reveal something similar, which is quite surprising for me. Is it difficult to implement this functionality ? ...

Examples of how to add an iterator to a custom class in Java 1.4?

Could someone give me an example of the best way to add an iterator to a custom class pre Java 5's iterable interface? Would wrapping a Collection be the best option? Something like public Iterator iterator() { return wrappedCollection.iterator(); } In my initial post I confused Iterator with Iterable. The end result I am interes...

How to erase & delete pointers to objects stored in a vector?

I have a vector that stores pointers to many objects instantiated dynamically, and I'm trying to iterate through the vector and remove certain elements (remove from vector and destroy object), but I'm having trouble. Here's what it looks like: vector<Entity*> Entities; /* Fill vector here */ vector<Entity*>::iterator it; ...

Java: adding elements to a collection during iteration

Is it possible to add elements to a collection while iterating over it? More specifically, I would like to iterate over a collection, and if an element satisfies a certain condition I want to add some other elements to the collection, and make sure that these added elements are iterated over as well. (I realise that this could lead to ...

C++ Container / Iterator Dependency Problem

Hello :) I'm working on an container class that looks something like this: class hexFile { public: HANDLE theFile; unsigned __int64 fileLength; hexFile(const std::wstring& fileName) { theFile = CreateFile(fileName.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_DELETE | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FI...

How can I make an iterator that never ends?

I was just wondering what the easiest way to iterate over a set indefinitely, i.e. when it reaches the end it next(); calls the first object. I'm assuming that this is not an already predefined function in Java, so just looking for the easiest way to implement this in Java. ...

How to refactor usage of an iterator

I have some code i'd like to refactor that uses a C# iterator (ie IEnumerable). Unfortunately, I can't see to quite figure out the best way to allow other functions to work with the iterator without causing it to restart the iterator. For example: NewLineEnumerator nle = new NewLineEnumerator(); while (bytesRead > 0) { var nlenum =...

How does STL algorithm work independent of Iterator type?

How does STL algorithm work independent of Iterator type? ...

What is the difference between an Iterator and a Generator?

What is the difference between an Iterator and a Generator? ...

Ruby blocks, java closures in C++

I am developing a program where I find myself doing this like this a lot: void Model::SetCollideMode( const std::string &m ) { Body *body; std::map<std::string, Body* >::iterator iter; for (iter=this->bodies.begin(); iter!=this->bodies.end(); iter++) { body = iter->second; body->SetCollideMode( m ); } } I have sev...

Can't step into iterator block whilst debugging (C#)

I'm trying to debug my code which is being executed from a unit test project, but when I try to step into a method, it just passes straight onto the next line and the breakpoint inside that method isn't hit. The method is on a class which is in a different project, but all the code is built in debug mode and I've tried cleaning and rebui...

dual iterator in one python object

In python, I am trying to write a class that support two different kind of iterator. Roughly speaking, this object contains a matrix of data and I want to have two different kind of iterator to support row iteration and column iteration. ...

Rails: An elegant way to display a message when there are no elements in database.

Hello, I realized that I'm writing a lot of code similar to this one: <% unless @messages.blank? %> <% @messages.each do |message| %> <%# code or partial to display the message %> <% end %> <% else %> You have no messages. <% end %> Is there any construct in Ruby and/or Rails that would let me skip that first condition? So...

Conceptual Problems with Iterator

Hello :) I'm trying to write my first iterator class / container type. Basically, I want to be able to iterate though a file, have the file converted to HEX on the fly, and pass the result into the boost::xpressive library. I don't want to do a one shot conversion to a string in RAM, because some of the files I need to be able to proces...

Can I increment an iterator by just adding a number?

Can I do normal computations with iterators, i.e. just increment it by adding a number? As an example, if I want to remove the element vec[3], can I just do this: std::vector<int> vec; for(int i = 0; i < 5; ++i){ vec.push_back(i); } vec.erase(vec.begin() + 3); // removes vec[3] element It works for me (g++), but I'm not sure i...

<operator missing when iterating through c++ map

The following code does not want to compile. See the included error message. Code: #include <map> #include <vector> #include <iostream> class MapHolder { public: std::map<std::vector<std::string>,MapHolder> m_map; void walk_through_map() { std::map<std::vector<std::string>,MapHolder>::iterator it; for(it = m_map.beg...