iterator

Using Iterators to hide internal container and achieve generic operation over a base container

I basically want to have a base container class which can return a generic iterator that can be used to traverse an instance of the container class, without needing to specify iterator templates. I think I cannot implement the base container over a class template, which would then require a traversal algorithm based on template classes w...

How do I require const_iterator semantics in a template function signature?

I am creating a constructor that will take a pair of input iterators. I want the method signature to have compile-time const semantics similar to: DataObject::DataObject(const char *begin, const char *end) However, I can't find any examples of this. For example, my STL implementation's range constructor for vector is defined as: tem...

Returning an iterator to an element in STL Container

how would you check if the iterator that was returned by the function points to something in container class? ...

Iterate Multiple std::vector

I've read here and other places that when iterating a std::vector using indexes you should: std::vector <int> x(20,1); for (std::vector<int>::size_type i = 0; i < x.size(); i++){ x[i]+=3; } But what if you are iterating two vectors of different types: std::vector <int> x(20,1); std::vector <double> y(20,1.0); for (std::vector<int>:...

A different service for my Flex app using Zend_Amf

I have an iterator service that works fine already and returns a correctly structured values to my flex application through my Zend Amf server $contacts = array(); mysql_connect( 'localhost', 'root', 'test' ); mysql_select_db( 'test' ); $res = mysql_query( 'SELECT * FROM contact' ); while( $contact = mysql_fetch_as...

Yield multiple objects at a time from an iterable object?

How can I yield multiple items at a time from an iterable object? For example, with a sequence of arbitrary length, how can I iterate through the items in the sequence, in groups of X consecutive items per iteration? (Question inspired by an answer which used this technique.) ...

Iterate through struct variables.

I want to get an iterator to struct variable to set a particular one on runtime according to enum ID. for example - struct { char _char; int _int; char* pchar; }; enum { _CHAR, //0 _INT, //1 PCHAR //2 }; int main() { int i = 1; //_INT //if i = 1 then set variable _int of struct to some value. } can you do that without if/e...

What to do of exceptions when implementing java.lang.Iterator

The java.lang.Iterator interface has 3 methods: hasNext, next and remove. In order to implement a read-only iterator, you have to provide an implementation for 2 of those: hasNext and next. My problem is that these methods does not declare any exceptions. So if my code inside the iteration process declares exceptions, I must enclose my ...

Designing an iterator wrapper

I wrote a class that wraps an iterator and returns transformed values on demand: // iterator-wrapper.h template<class Iter, class Val, class Fct> class IteratorWrapper { Iter cur_; const Iter last_; const Fct fct_; public: IteratorWrapper(Iter first, Iter last, const Fct fct) : cur_(first), last_(last), fct_(fct) {} con...

C++ functor to output iterator adapter

Given a functor appropriate for use with std::for_each and friends: template <typename T> struct Foo { void operator()(T const& t) { ... } }; std::for_each(v.begin(), v.end(), Foo<Bar>()); Is there some standard way to convert this into an output iterator appropriate for use with std::copy and friends? (or the opposite adaptatio...

Is there any way to make a yield created Iterator Continue to the next item upon an Exception?

Hello Is there any way to have a yield created iterator continue to the next item when an exception occurs inside one of the iterator blocks? This is currently not working: Boolean result; while (true) { try { result = enumerator.MoveNext(); //Taken from a yield created e...

stl insertion iterators

Maybe I am missing something completely obvious, but I can't figure out why one would use back_inserter/front_inserter/inserter, instead of just providing the appropriate iterator from the container interface. And thats my question. ...

proper way to iterate through data in a class when the data changes asynchronously

I have a C# class which has a dictionary inside it. The class listens asynchronously to messages that, upon arrival and processing, alter the data in the dictionary. What is the "correct" way for an app that uses an instance of this class to iterate through the data in the dictionary? Do I have to wrap it into another class and implemen...

Why you cannot use an unsafe keyword in an iterator context?

In looking at this question which Jon did a fine job in answering... 'How to read a text file reversly with iterator'. And there was a similar question in which I answered using pointers hocus pocus..'.net is there a way to read a text file from bottom to top' before it got closed.... Now I did set out to try solve this using pointers, ...

Constant correctness

In the printMessage if you access the vector of a constant class using the index it works fine, but not with the the iterator (*itr). If the iterator is declared as constant_iterator then it works fine. Why? In both cases I am reading the data and not modifying the vector. Can someone shed some light? #include <iostream> #inclu...

each() and list() function

I don't understand the each() and the list() function that well. Can anyone please give me a little more detail and explain to me how can it be useful? Edit: <?php $foo = array("bob", "fred", "jussi", "jouni", "egon", "marliese"); $bar = each($foo); print_r($bar); ?> Array ( [1] => bob [value] => bob [0] => 0 [key] => ...

Iterator Concurrent Modifiction Exception

This code will throw Concurrent Modification Exception if the list is modified in doSomething(). Is it possible to avoid it by enclosing the code in some synchronized block? List l = Collections.synchronizedList(new ArrayList()); // normal iteration -- can throw ConcurrentModificationException // may require external synchronization fo...

Iterating & containers of smart pointers

I have a container of smart pointers to mutable objects. I have to write two *for_each* loops, one for accessing the objects as read-only data and another for mutable data. The compiler is telling me that std::vector< boost::shared_ptr<Object> > is not the same as std::vector< boost::shared_ptr<const Object> >, note the const. Here ...

How to use iterators in java?

I have implemented Priority Queue interface for making heap. Can you tell me how to implement an iterator on the top of that? point me to some apropriate tutorial,i am new to java and on a very short deadline here. Actually i need a method to find and modify an object from heap on the basis of Object.id. I dont care if it is O(n). publi...

How to construct a std::list iterator in loop with increment

I'm trying to do a double-loop over a std::list to operate on each pair of elements. However, I'm having some trouble initialising the second iterator. The code I'd like to write is: for(std::list<int>::iterator i = l.begin(); i != l.end(); ++i) { for(std::list<int>::iterator j = i+1; j != l.end(); ++j) { ... } } That ...